日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

C#--检索线程状态

發布時間:2025/6/17 C# 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C#--检索线程状态 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Thread對象的生存期特征由一組狀態描述。Thread對象的ThreadState屬性將返回下列10個數值中的一個:

  • Unstarted  線程尚未開始
  • Running   線程正則執行
  • Background  線程正在后臺執行
  • WaitSleepJoin  線程由于調用Wait、Sleep或Join方法而被阻塞
  • SuspendRequested  線程對象位于當前掛起的進程中
  • Suspended  線程對象已經停止
  • StopRequested  線程對象位于當前停止的進程中
  • Stopped  線程對象已經停止
  • AbortRequested  Abort方位已被調用,但是線程對象尚未收到ThreadAbortException
  • Aborted  線程對象被異常終止

?????Thread對象可以在某一時刻處于多種狀態。例如,某個正在等待資源的Thread對象,如果在它等待時調用了Abort方法,那么該線程對象可能同時處于WaitSleepJoin和AbortRequested狀態。

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 }

轉載于:https://www.cnblogs.com/djcsch2001/archive/2011/05/10/2042456.html

總結

以上是生活随笔為你收集整理的C#--检索线程状态的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。