表示線程同步事件在一個等待線程釋放後收到信號時自動重置.android
1 using System; 2 using System.Threading; 3 4 // Visual Studio: Replace the default class in a Console project with 5 // the following class. 6 class Example 7 { 8 private static AutoResetEvent event_1 = new AutoResetEvent(true); 9 private static AutoResetEvent event_2 = new AutoResetEvent(false); 10 11 static void Main() 12 { 13 Console.WriteLine("Press Enter to create three threads and start them.\r\n" + 14 "The threads wait on AutoResetEvent #1, which was created\r\n" + 15 "in the signaled state, so the first thread is released.\r\n" + 16 "This puts AutoResetEvent #1 into the unsignaled state."); 17 Console.ReadLine(); 18 19 for (int i = 1; i < 4; i++) 20 { 21 Thread t = new Thread(ThreadProc); 22 t.Name = "Thread_" + i; 23 t.Start(); 24 } 25 Thread.Sleep(250); 26 27 for (int i = 0; i < 2; i++) 28 { 29 Console.WriteLine("Press Enter to release another thread."); 30 Console.ReadLine(); 31 event_1.Set(); 32 Thread.Sleep(250); 33 } 34 35 Console.WriteLine("\r\nAll threads are now waiting on AutoResetEvent #2."); 36 for (int i = 0; i < 3; i++) 37 { 38 Console.WriteLine("Press Enter to release a thread."); 39 Console.ReadLine(); 40 event_2.Set(); 41 Thread.Sleep(250); 42 } 43 44 // Visual Studio: Uncomment the following line. 45 //Console.Readline(); 46 } 47 48 static void ThreadProc() 49 { 50 string name = Thread.CurrentThread.Name; 51 52 Console.WriteLine("{0} waits on AutoResetEvent #1.", name); 53 event_1.WaitOne(); 54 Console.WriteLine("{0} is released from AutoResetEvent #1.", name); 55 56 Console.WriteLine("{0} waits on AutoResetEvent #2.", name); 57 event_2.WaitOne(); 58 Console.WriteLine("{0} is released from AutoResetEvent #2.", name); 59 60 Console.WriteLine("{0} ends.", name); 61 } 62 } 63 64 /* This example produces output similar to the following: 65 66 Press Enter to create three threads and start them. 67 The threads wait on AutoResetEvent #1, which was created 68 in the signaled state, so the first thread is released. 69 This puts AutoResetEvent #1 into the unsignaled state. 70 71 Thread_1 waits on AutoResetEvent #1. 72 Thread_1 is released from AutoResetEvent #1. 73 Thread_1 waits on AutoResetEvent #2. 74 Thread_3 waits on AutoResetEvent #1. 75 Thread_2 waits on AutoResetEvent #1. 76 Press Enter to release another thread. 77 78 Thread_3 is released from AutoResetEvent #1. 79 Thread_3 waits on AutoResetEvent #2. 80 Press Enter to release another thread. 81 82 Thread_2 is released from AutoResetEvent #1. 83 Thread_2 waits on AutoResetEvent #2. 84 85 All threads are now waiting on AutoResetEvent #2. 86 Press Enter to release a thread. 87 88 Thread_2 is released from AutoResetEvent #2. 89 Thread_2 ends. 90 Press Enter to release a thread. 91 92 Thread_1 is released from AutoResetEvent #2. 93 Thread_1 ends. 94 Press Enter to release a thread. 95 96 Thread_3 is released from AutoResetEvent #2. 97 Thread_3 ends. 98 */
AutoResetEvent(Boolean) | 用一個指示是否將初始狀態設置爲終止的布爾值初始化 AutoResetEvent 類的新實例。ios |
Close() | 釋放由當前 WaitHandle 佔用的全部資源。api (Inherited from WaitHandle) |
CreateObjRef(Type) | 建立一個對象,該對象包含生成用於與遠程對象進行通訊的代理所需的所有相關信息。app (Inherited from MarshalByRefObject) |
Dispose() | 釋放 WaitHandle 類的當前實例所使用的全部資源。函數 (Inherited from WaitHandle) |
Dispose(Boolean) | 當在派生類中重寫時,釋放 WaitHandle 使用的非託管資源,而且可選擇釋放託管資源。spa (Inherited from WaitHandle) |
Equals(Object) | 肯定指定的對象是否等於當前對象。操作系統 (Inherited from Object) |
GetHashCode() | 做爲默認哈希函數。線程 (Inherited from Object) |
GetLifetimeService() | 檢索控制此實例的生存期策略的當前生存期服務對象。代理 (Inherited from MarshalByRefObject) |
GetType() | 獲取當前實例的 Type。code (Inherited from Object) |
InitializeLifetimeService() | 獲取生存期服務對象來控制此實例的生存期策略。 (Inherited from MarshalByRefObject) |
MemberwiseClone() | 建立當前 Object 的淺表副本。 (Inherited from Object) |
MemberwiseClone(Boolean) | 建立當前 MarshalByRefObject 對象的淺表副本。 (Inherited from MarshalByRefObject) |
ToString() | 返回表示當前對象的字符串。 (Inherited from Object) |
WaitOne() | 阻止當前線程,直到當前 WaitHandle 收到信號。 (Inherited from WaitHandle) |
WaitOne(Int32) | 阻止當前線程,直到當前 WaitHandle 收到信號,同時使用 32 位帶符號整數指定時間間隔(以毫秒爲單位)。 (Inherited from WaitHandle) |
WaitOne(Int32, Boolean) | 阻止當前線程,直到當前的 WaitHandle 收到信號爲止,同時使用 32 位帶符號整數指定時間間隔,並指定是否在等待以前退出同步域。 (Inherited from WaitHandle) |
WaitOne(TimeSpan) | 阻止當前線程,直到當前實例收到信號,同時使用 TimeSpan 指定時間間隔。 (Inherited from WaitHandle) |
WaitOne(TimeSpan, Boolean) | 阻止當前線程,直到當前實例收到信號爲止,同時使用 TimeSpan 指定時間間隔,並指定是否在等待以前退出同步域。 (Inherited from WaitHandle) |
Handle | 獲取或設置本機操做系統句柄。 (Inherited from WaitHandle) |
SafeWaitHandle | 獲取或設置本機操做系統句柄。 (Inherited from WaitHandle) |
IDisposable.Dispose() | 釋放由 WaitHandle 使用的全部資源。 (Inherited from WaitHandle) |
WaitTimeout | 指示在任何等待句柄終止以前 WaitAny(WaitHandle[], Int32, Boolean) 操做已超時。此字段爲常數。 (Inherited from WaitHandle) |