C#--检索线程状态
生活随笔
收集整理的這篇文章主要介紹了
C#--检索线程状态
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Thread對(duì)象的生存期特征由一組狀態(tài)描述。Thread對(duì)象的ThreadState屬性將返回下列10個(gè)數(shù)值中的一個(gè):
- Unstarted 線程尚未開(kāi)始
- Running 線程正則執(zhí)行
- Background 線程正在后臺(tái)執(zhí)行
- WaitSleepJoin 線程由于調(diào)用Wait、Sleep或Join方法而被阻塞
- SuspendRequested 線程對(duì)象位于當(dāng)前掛起的進(jìn)程中
- Suspended 線程對(duì)象已經(jīng)停止
- StopRequested 線程對(duì)象位于當(dāng)前停止的進(jìn)程中
- Stopped 線程對(duì)象已經(jīng)停止
- AbortRequested Abort方位已被調(diào)用,但是線程對(duì)象尚未收到ThreadAbortException
- Aborted 線程對(duì)象被異常終止
?????Thread對(duì)象可以在某一時(shí)刻處于多種狀態(tài)。例如,某個(gè)正在等待資源的Thread對(duì)象,如果在它等待時(shí)調(diào)用了Abort方法,那么該線程對(duì)象可能同時(shí)處于WaitSleepJoin和AbortRequested狀態(tài)。
1 /*2 Example14_3.cs illustrates the ThreadState property
3 */
4
5 using System;
6 using System.Threading;
7
8 class Example14_3
9 {
10
11 // the Countdown method counts down from 10 to 1
12 public static void Countdown()
13 {
14 for (int counter = 10; counter > 0; counter--)
15 {
16 Console.Write(counter.ToString() + " ");
17 }
18 Console.WriteLine();
19 }
20
21 // the DumpThreadState method displays the current Thread's state
22 // Note that ThreadState is a bitmask, and multiple states for the
23 // same thread are valid
24 public static void DumpThreadState (
25 Thread t
26 )
27 {
28 Console.Write("Current state: ");
29 if ((t.ThreadState & ThreadState.Aborted) == ThreadState.Aborted)
30 Console.Write("Aborted ");
31 if ((t.ThreadState & ThreadState.AbortRequested) ==
32 ThreadState.AbortRequested)
33 Console.Write("AbortRequested ");
34 if ((t.ThreadState & ThreadState.Background) ==
35 ThreadState.Background)
36 Console.Write("Background ");
37 if ((t.ThreadState &
38 (ThreadState.Stopped | ThreadState.Unstarted |
39 ThreadState.Aborted)) == 0)
40 Console.Write("Running ");
41 if ((t.ThreadState & ThreadState.Stopped) == ThreadState.Stopped)
42 Console.Write("Stopped ");
43 if ((t.ThreadState & ThreadState.StopRequested) ==
44 ThreadState.StopRequested)
45 Console.Write("StopRequested ");
46 if ((t.ThreadState & ThreadState.Suspended) ==
47 ThreadState.Suspended)
48 Console.Write("Suspended ");
49 if ((t.ThreadState & ThreadState.SuspendRequested) ==
50 ThreadState.SuspendRequested)
51 Console.Write("SuspendRequested ");
52 if ((t.ThreadState & ThreadState.Unstarted) ==
53 ThreadState.Unstarted)
54 Console.Write("Unstarted ");
55 if ((t.ThreadState & ThreadState.WaitSleepJoin) ==
56 ThreadState.WaitSleepJoin)
57 Console.Write("WaitSleepJoin ");
58 Console.WriteLine();
59 }
60
61 public static void Main()
62 {
63
64 // create a second thread
65 Thread t2 = new Thread(new ThreadStart(Countdown));
66 DumpThreadState(t2);
67
68 // launch the second thread
69 t2.Start();
70 DumpThreadState(t2);
71
72 // and meanwhile call the Countdown method from the first thread
73 Countdown();
74
75 // shut down the second thread
76 t2.Abort();
77 DumpThreadState(t2);
78
79 }
80
81 }
轉(zhuǎn)載于:https://www.cnblogs.com/djcsch2001/archive/2011/05/10/2042456.html
總結(jié)
以上是生活随笔為你收集整理的C#--检索线程状态的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: poj 3036
- 下一篇: Step by Step WebMatr