Home »

How do I spawn a thread?

Question ListCategory: ASP.NETHow do I spawn a thread?
adamemliy16 author asked 2 years ago
1 Answers
jeanderson295 author answered 1 year ago

Create an instance of a System.Threading.Thread object, passing it aninstance of a ThreadStart delegate that will be executed on the new thread.
For example:
class MyThread
{
public MyThread( string initData )
{
m_data = initData;
m_thread = new Thread( new ThreadStart(ThreadMain) );
m_thread.Start();
}
// ThreadMain() is executed on the new thread.
private void ThreadMain()
{
Console.WriteLine( m_data );
}
public void WaitUntilFinished()
{
m_thread.Join();
}
Satish Marwat Dot Net Web Resources satishcm@gmail.com 37 Page
private Thread m_thread;
private string m_data;
}
In this case creating an instance of the MyThread class is sufficient to spawn
the thread and execute the MyThread.ThreadMain() method:
MyThread t = new MyThread( “Hello, world.” );
t.WaitUntilFinished();

Please login or Register to Submit Answer