An alternate way to start a thread: start a thread in its own constructor : Thread Start « Thread « C# / CSharp Tutorial






using System; 
using System.Threading; 
 
class MyThread { 
  public int count; 
  public Thread thrd; 
 
  public MyThread(string name) { 
    count = 0; 
    thrd = new Thread(this.run);
    thrd.Name = name; 
    thrd.Start(); 
  } 
 
  void run() { 
    Console.WriteLine(thrd.Name + " starting."); 
 
    do { 
      Thread.Sleep(500); 
      Console.WriteLine("In " + thrd.Name + 
                        ", count is " + count); 
      count++; 
    } while(count < 10); 
 
    Console.WriteLine(thrd.Name + " terminating."); 
  } 
} 
 
class MainClass { 
  public static void Main() { 
    Console.WriteLine("Main thread starting."); 
 
    MyThread mt = new MyThread("Child #1"); 
 
    do { 
      Console.Write("."); 
      Thread.Sleep(100); 
    } while (mt.count != 10); 
 
    Console.WriteLine("Main thread ending."); 
  } 
}
Main thread starting.
Child #1 starting.
.....In Child #1, count is 0
.....In Child #1, count is 1
....In Child #1, count is 2
.....In Child #1, count is 3
....In Child #1, count is 4
.....In Child #1, count is 5
.....In Child #1, count is 6
....In Child #1, count is 7
.....In Child #1, count is 8
....In Child #1, count is 9
Child #1 terminating.
Main thread ending.








20.2.Thread Start
20.2.1.An alternate way to start a thread: start a thread in its own constructor
20.2.2.thread.IsAlive
20.2.3.Simple Thread with ThreadStart