Csharp/C Sharp by API/System.Threading/ParameterizedThreadStart

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

new ParameterizedThreadStart

<source lang="csharp">

using System; using System.Threading;

class MyThread {

 public int count;  
 public Thread thrd;  

 public MyThread(string name, int num) {  
   count = 0;  

   thrd = new Thread(new ParameterizedThreadStart(this.run)); 

   thrd.Name = name; 

   thrd.Start(num);  
 }  
 
 void run(object num) {  
   Console.WriteLine(thrd.Name + " starting with count of " + num);  
 
   do {  
     Thread.Sleep(500);  
     Console.WriteLine("In " + thrd.Name + ", count is " + count);  
     count++;  
   } while(count < (int) num);  
 
   Console.WriteLine(thrd.Name + " terminating.");  
 }  

}

class MainClass {

 public static void Main() {  
   MyThread mt = new MyThread("Child #1", 5);  
   MyThread mt2 = new MyThread("Child #1", 3);  
 
   do {  
     Thread.Sleep(100);  
   } while (mt.thrd.IsAlive | mt2.thrd.IsAlive);  
 
   Console.WriteLine("Main thread ending.");  
 }  

}

</source>