溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

C#中如何使用ManualResetEvent

發(fā)布時間:2021-07-07 17:55:57 來源:億速云 閱讀:112 作者:Leah 欄目:編程語言

這篇文章將為大家詳細講解有關C#中如何使用ManualResetEvent,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。

ManualResetEvent表示線程同步事件,可以對所有進行等待的線程進行統(tǒng)一管理(收到信號時必須手動重置該事件)

其構造函數為:

public ManualResetEvent (bool initialState);

參數 initialState 表示是否初始化,如果為 true,則將初始狀態(tài)設置為終止(不阻塞);如果為 false,則將初始狀態(tài)設置為非終止(阻塞)。

注意:如果其參數設置為true,則ManualResetEvent等待的線程不會阻塞。 如果初始狀態(tài)為false, 則在Set調用方法之前, 將阻止線程。

它只有兩個方法

//將事件狀態(tài)設置為終止狀態(tài),從而允許繼續(xù)執(zhí)行一個或多個等待線程。public bool Set ();//將事件狀態(tài)設置為非終止,從而導致線程受阻。public bool Reset ();//返回值:操作成功返回true,否則false

講了這么多,看個例子理解一下

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");    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();    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();    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();    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();    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(); //阻塞線程,直到調用Set方法才能繼續(xù)執(zhí)行    Console.WriteLine(name + " ends.");  }}

結果如下

過程:

該示例以信號狀態(tài)ManualResetEvent( false即傳遞給構造函數的) 開頭。 三個線程, 每個線程在調用其WaitOne方法時被阻止。 當用戶按Enter鍵時, 該示例調用Set方法, 該方法釋放所有三個線程,使其繼續(xù)執(zhí)行。

再次按 " enter " 鍵, 此時ManualResetEvent在調用Reset方法之前, 一直保持終止狀態(tài),因此這些線程在調用WaitOne方法時不會被阻止, 而是運行到完成。即對應上述(收到信號時必須手動重置該事件)

再次按enter鍵將導致該示例調用Reset方法, 并啟動一個線程, 該線程在調用WaitOne時將被阻止。 按enter鍵, 最后一次調用Set以釋放最后一個線程, 程序結束。

關于C#中如何使用ManualResetEvent就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI