C#中如何使用ManualResetEvent

這篇文章將為大家詳細(xì)講解有關(guān)C#中如何使用ManualResetEvent,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。

10多年的潯陽(yáng)網(wǎng)站建設(shè)經(jīng)驗(yàn),針對(duì)設(shè)計(jì)、前端、開發(fā)、售后、文案、推廣等六對(duì)一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。營(yíng)銷型網(wǎng)站建設(shè)的優(yōu)勢(shì)是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整潯陽(yáng)建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無(wú)論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。創(chuàng)新互聯(lián)公司從事“潯陽(yáng)網(wǎng)站設(shè)計(jì)”,“潯陽(yáng)網(wǎng)站推廣”以來(lái),每個(gè)客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。

ManualResetEvent表示線程同步事件,可以對(duì)所有進(jìn)行等待的線程進(jìn)行統(tǒng)一管理(收到信號(hào)時(shí)必須手動(dòng)重置該事件)

其構(gòu)造函數(shù)為:

public ManualResetEvent (bool initialState);

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

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

它只有兩個(gè)方法

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

講了這么多,看個(gè)例子理解一下

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

結(jié)果如下

過程:

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

再次按 " enter " 鍵, 此時(shí)ManualResetEvent在調(diào)用Reset方法之前, 一直保持終止?fàn)顟B(tài),因此這些線程在調(diào)用WaitOne方法時(shí)不會(huì)被阻止, 而是運(yùn)行到完成。即對(duì)應(yīng)上述(收到信號(hào)時(shí)必須手動(dòng)重置該事件)

再次按enter鍵將導(dǎo)致該示例調(diào)用Reset方法, 并啟動(dòng)一個(gè)線程, 該線程在調(diào)用WaitOne時(shí)將被阻止。 按enter鍵, 最后一次調(diào)用Set以釋放最后一個(gè)線程, 程序結(jié)束。

關(guān)于C#中如何使用ManualResetEvent就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

當(dāng)前文章:C#中如何使用ManualResetEvent
鏈接URL:http://muchs.cn/article0/iheeoo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)公司、App開發(fā)、標(biāo)簽優(yōu)化移動(dòng)網(wǎng)站建設(shè)、商城網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

營(yíng)銷型網(wǎng)站建設(shè)