Csharp/CSharp Tutorial/Thread/Thread State

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

ThreadState property

<source lang="csharp">using System; using System.Threading; class MainClass {

 public static void Countdown() 
 {
   for (int i = 10; i > 0; i--)  {
     Console.Write(i.ToString() + " ");
   }
 }
 public static void DumpThreadState (Thread t ) 
 {
   Console.Write("Current state: ");
   if ((t.ThreadState & ThreadState.Aborted) == ThreadState.Aborted)
     Console.Write("Aborted ");
   if ((t.ThreadState & ThreadState.AbortRequested) == ThreadState.AbortRequested)
     Console.Write("AbortRequested ");
   if ((t.ThreadState & ThreadState.Background) == ThreadState.Background)
     Console.Write("Background ");
   if ((t.ThreadState & (ThreadState.Stopped | ThreadState.Unstarted | ThreadState.Aborted)) == 0)
     Console.Write("Running ");
   if ((t.ThreadState & ThreadState.Stopped) == ThreadState.Stopped)
     Console.Write("Stopped ");
   if ((t.ThreadState & ThreadState.StopRequested) == ThreadState.StopRequested)
     Console.Write("StopRequested ");
   if ((t.ThreadState & ThreadState.Suspended) == ThreadState.Suspended)
     Console.Write("Suspended ");
   if ((t.ThreadState & ThreadState.SuspendRequested) == ThreadState.SuspendRequested)
     Console.Write("SuspendRequested ");
   if ((t.ThreadState & ThreadState.Unstarted) == ThreadState.Unstarted)
     Console.Write("Unstarted ");
   if ((t.ThreadState & ThreadState.WaitSleepJoin) == ThreadState.WaitSleepJoin)
     Console.Write("WaitSleepJoin ");
 }
 public static void Main() 
 {
   Thread t2 = new Thread(new ThreadStart(Countdown));
   DumpThreadState(t2);
   t2.Start();
   DumpThreadState(t2);
   Countdown();
   t2.Abort();
   DumpThreadState(t2);
 }

}</source>

Current state: Unstarted 10 9 8 7 6 5 4 3 2 1 Current state: Stopped 10 9 8 7 6 5 4 3 2 1 Current st
ate: Stopped

Use IsAlive to wait for threads to end

<source lang="csharp">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 mt1 = new MyThread("Child #1"); 
   MyThread mt2 = new MyThread("Child #2"); 
   MyThread mt3 = new MyThread("Child #3"); 

   do { 
     Console.Write("."); 
     Thread.Sleep(100); 
   } while (mt1.thrd.IsAlive && 
            mt2.thrd.IsAlive && 
            mt3.thrd.IsAlive); 

   Console.WriteLine("Main thread ending."); 
 } 

}</source>

Main thread starting.
Child #1 starting.
Child #2 starting.
Child #3 starting.
.....In Child #1, count is 0
In Child #2, count is 0
In Child #3, count is 0
.....In Child #1, count is 1
In Child #2, count is 1
In Child #3, count is 1
....In Child #1, count is 2
In Child #2, count is 2
In Child #3, count is 2
.....In Child #1, count is 3
In Child #2, count is 3
In Child #3, count is 3
....In Child #1, count is 4
In Child #2, count is 4
In Child #3, count is 4
.....In Child #1, count is 5
In Child #2, count is 5
In Child #3, count is 5
.....In Child #1, count is 6
In Child #2, count is 6
In Child #3, count is 6
....In Child #1, count is 7
In Child #2, count is 7
In Child #3, count is 7
.....In Child #1, count is 8
In Child #2, count is 8
In Child #3, count is 8
....In Child #1, count is 9
Child #1 terminating.
In Child #2, count is 9
Child #2 terminating.
In Child #3, count is 9
Child #3 terminating.
Main thread ending.