C#中如何使用ManualResetEvent

这篇文章将为大家详细讲解有关C#中如何使用ManualResetEvent,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

10多年的浔阳网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。营销型网站建设的优势是能够根据用户设备显示端的尺寸不同,自动调整浔阳建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联公司从事“浔阳网站设计”,“浔阳网站推广”以来,每个客户项目都认真落实执行。

ManualResetEvent表示线程同步事件,可以对所有进行等待的线程进行统一管理(收到信号时必须手动重置该事件)

其构造函数为:

public ManualResetEvent (bool initialState);

参数 initialState 表示是否初始化,如果为 true,则将初始状态设置为终止(不阻塞);如果为 false,则将初始状态设置为非终止(阻塞)。

注意:如果其参数设置为true,则ManualResetEvent等待的线程不会阻塞。 如果初始状态为false, 则在Set调用方法之前, 将阻止线程。

它只有两个方法

//将事件状态设置为终止状态,从而允许继续执行一个或多个等待线程。public bool Set ();//将事件状态设置为非终止,从而导致线程受阻。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方法才能继续执行    Console.WriteLine(name + " ends.");  }}

结果如下

过程:

该示例以信号状态ManualResetEvent( false即传递给构造函数的) 开头。 三个线程, 每个线程在调用其WaitOne方法时被阻止。 当用户按Enter键时, 该示例调用Set方法, 该方法释放所有三个线程,使其继续执行。

再次按 " enter " 键, 此时ManualResetEvent在调用Reset方法之前, 一直保持终止状态,因此这些线程在调用WaitOne方法时不会被阻止, 而是运行到完成。即对应上述(收到信号时必须手动重置该事件)

再次按enter键将导致该示例调用Reset方法, 并启动一个线程, 该线程在调用WaitOne时将被阻止。 按enter键, 最后一次调用Set以释放最后一个线程, 程序结束。

关于C#中如何使用ManualResetEvent就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

当前文章:C#中如何使用ManualResetEvent
链接URL:https://www.cdcxhl.com/article0/iheeoo.html

成都网站建设公司_创新互联,为您提供外贸网站建设网站设计公司App开发标签优化移动网站建设商城网站

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

营销型网站建设