定義
ManualResetEvent 類
命名空間:System.Threading
程序集:mscorlib.dll
表示線程同步事件,收到信號時,必須手動重置該事件。 無法繼承此類。
用於對於所有線程的控制:
常見使用方法:
1、初始化;
//true-初始狀態爲發出信號;false-初始狀態爲未發出信號,一般設置爲false,手動來發出信號
ManualResetEvent mre = new ManualResetEvent(false);
2、阻塞
mre.WaitOne(); //所有線程會被阻塞,直到set後,才執行WaitOne()後的代碼
3、解除阻塞
mre.Set(); //當你想要的狀態達到了,就接觸阻塞,繼續執行線程後面的代碼
//備註: Set()後,如果再調用WaitOne()是無效的,線程不會被阻塞,必須調用Reset()才行
4、重置
mre.Reset(); //重置,只有這樣,當執行WaitOne()的時候纔會再次被阻塞
最後上一個官方的案例:
using System;
using System.Threading;
public class Example
{
// mre is used to block and release threads manually. It is
// created in the unsignaled state.
private static ManualResetEvent mre = new ManualResetEvent(false);
static void Main()
{
Console.WriteLine("\nStart 3 named threads that block on a ManualResetEvent:\n");
//中文註釋1:開啓三個線程,每個線程開啓後調用WaitOne()阻塞。
for(int i = 0; i <= 2; i++)
{
Thread t = new Thread(ThreadProc);
t.Name = "Thread_" + i;
t.Start();
}
Thread.Sleep(500);
Console.WriteLine("\nWhen all three threads have started, press Enter to call Set()" +
"\nto release all the threads.\n");
Console.ReadLine();
//中文註釋2:只有當Set()後纔會執行WaitOne()後的代碼
mre.Set();
Thread.Sleep(500);
Console.WriteLine("\nWhen a ManualResetEvent is signaled, threads that call WaitOne()" +
"\ndo not block. Press Enter to show this.\n");
Console.ReadLine();
//中文註釋3:繼續再開兩個線程,仍然調用WaitOne(),但是不會阻塞,會繼續執行
for(int i = 3; i <= 4; i++)
{
Thread t = new Thread(ThreadProc);
t.Name = "Thread_" + i;
t.Start();
}
Thread.Sleep(500);
Console.WriteLine("\nPress Enter to call Reset(), so that threads once again block" +
"\nwhen they call WaitOne().\n");
Console.ReadLine();
//中文註釋4:只有Reset()後,下面再開線程就會繼續被阻塞
mre.Reset();
// Start a thread that waits on the ManualResetEvent.
Thread t5 = new Thread(ThreadProc);
t5.Name = "Thread_5";
t5.Start();
Thread.Sleep(500);
Console.WriteLine("\nPress Enter to call Set() and conclude the demo.");
Console.ReadLine();
//中文註釋5:再次Set(),就可以了
mre.Set();
// If you run this example in Visual Studio, uncomment the following line:
//Console.ReadLine();
}
private static void ThreadProc()
{
string name = Thread.CurrentThread.Name;
Console.WriteLine(name + " starts and calls mre.WaitOne()");
mre.WaitOne();
Console.WriteLine(name + " ends.");
}
}
/* This example produces output similar to the following:
Start 3 named threads that block on a ManualResetEvent:
Thread_0 starts and calls mre.WaitOne()
Thread_1 starts and calls mre.WaitOne()
Thread_2 starts and calls mre.WaitOne()
When all three threads have started, press Enter to call Set()
to release all the threads.
Thread_2 ends.
Thread_0 ends.
Thread_1 ends.
When a ManualResetEvent is signaled, threads that call WaitOne()
do not block. Press Enter to show this.
Thread_3 starts and calls mre.WaitOne()
Thread_3 ends.
Thread_4 starts and calls mre.WaitOne()
Thread_4 ends.
Press Enter to call Reset(), so that threads once again block
when they call WaitOne().
Thread_5 starts and calls mre.WaitOne()
Press Enter to call Set() and conclude the demo.
Thread_5 ends.
*/
番外:
對比 ManualResetEvent 和 AutoResetEvent
舉例:
AutoResetEvent :地鐵口刷卡通過,每次刷卡視爲一次Set(),每次只過一個人(一個線程),一般是用來同步訪問資源。
ManualResetEvent :景點大門,每次Set()視爲打開大門,很多人都可以進,無論先來的還是後來的。直到Reset()關閉大門,都不能進了。