ManualResetEven使用的最清楚说明

快速阅读

理解ManualResetEvent,以及如何使用。

官方说明

官方介绍:https://docs.microsoft.com/en-us/dotnet/api/system.threading.manualresetevent?view=netframework-1.1

一个线程同步事件 ,通过发信号来控制线程来控制 是否有权限访问 资源

构造函数

初始化实例,并且指明信号的初始状态 。

private static ManualResetEvent mre = new ManualResetEvent(false);

true:表示线程可以访问资源 ,就是可以无视waitone里的等待

false:表示线程如果碰到waitone的时候 ,就要暂停,等主线程来控制 。

比如如下demo中,主线程调用线程执行以下方法时,如果默认是false,则只会输入***starts and calls mre.WaitOne() 而没有 ___ends的输出,因为默认是false ,线程中的waitone()会阻止线程的继续访问 。

private static void ThreadProc()
{
string name = Thread.CurrentThread.Name; Console.WriteLine(name + " starts and calls mre.WaitOne()"); mre.WaitOne(); Console.WriteLine(name + " ends.");
}

Set()方法

将事件状态设置为“已发送信号”,允许被waitone() 阻止的一个或者多个线程进行执行线程中的代码。

这个上面的demo中就会在调用mre.set()方法执行之后,会继续调用线程中的下面的___ends 的输出。

Reset()方法

将事件状态设置为“没信号”,这样线程中执行waitone()的时候 ,就会阴止当前线程的执行。实现了和构造函数传入默认值false一样的效果,不过它可以在执行的过程中,进行重新设置,表求又把线程调用组止了。

直接再次接收到set方法 。才会再次执行下面的访问 。

官方的demo

private static ManualResetEvent mre = new ManualResetEvent(false);

static void Main(string[] args)
{
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();
} private static void ThreadProc()
{
string name = Thread.CurrentThread.Name; Console.WriteLine(name + " starts and calls mre.WaitOne()"); mre.WaitOne(); Console.WriteLine(name + " ends.");
}

运行结果

友情提示

​ 我对我的文章负责,发现好多网上的文章 没有实践,都发出来的,让人走很多弯路,如果你在我的文章中遇到无法实现,或者无法走通的问题。可以直接在公众号《爱码农爱生活 》留言。必定会再次复查原因。让每一篇 文章的流程都能顺利实现。

ManualResetEven使用的最清楚说明的更多相关文章

  1. C#学习笔记之线程 - 通知Signal

    通知事件等待句柄 Signal With EventWaitHandle 事件等待句柄常用于通知.当一个线程等待直到接收到另外一个线程发出的信号.事件等待句柄是最简单的信号结构,它与C#事件无关.有三 ...

随机推荐

  1. FreeRTOS队列操作

    API函数 //创建 #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) #define xQueueCreate( uxQueueLength, uxItemS ...

  2. [AIR] NativeExtension在IOS下的开发实例 --- ANE文件的打包(三)

    来源:http://bbs.9ria.com/thread-102041-1-1.html 好了,前面的准备工作做的差不多了.此时我们应用有下面几个文件:extension.xml    CoolEx ...

  3. Crypto模块中的签名算法

    因为支付宝当中需要自行实现签名,所以就用到了SHA265和RSA2,将拼接好的信息用私钥进行签名,并进行Base64编码,然后解密就用支付宝回传给用户的公钥解密就ok了,所以我就使用Crypto模块, ...

  4. [dev][nginx] 在阅读nginx代码之前都需要准备什么

    前言 以前,我读过nginx的源码,甚至还改过.但是,现在回想起来几乎回想不起任何东西, 只记得到处都是回调和异步,我的vim+ctags索引起来十分吃力. 几乎没有任何收获,都是因为当时打开代码就看 ...

  5. React官方中文文档【安装】

    https://reactjs.org/docs/getting-started.html  //React官方文档地址 1.入门 此页面是React文档和相关资源的概述. React是一个用于构建用 ...

  6. MyBatis_tp50_动态sql_sql标签_抽取可重用的sql片段_使用include标签进行引用

    笔记要点出错分析与总结 include内部使用自定的属性,之能使用$ {}来取值 ,#{}不能用 工程组织数据库组织0.重新修改Bean类1.定义接口 public interface Employe ...

  7. 《你说对就队》第八次团队作业:Alpha冲刺 第三天

    <你说对就队>第八次团队作业:Alpha冲刺 第三天 项目 内容 这个作业属于哪个课程 [教师博客主页链接] 这个作业的要求在哪里 [作业链接地址] 团队名称 <你说对就队> ...

  8. machine learning (7)---normal equation相对于gradient descent而言求解linear regression问题的另一种方式

    Normal equation: 一种用来linear regression问题的求解Θ的方法,另一种可以是gradient descent 仅适用于linear regression问题的求解,对其 ...

  9. Samba服务安装

    安装Samba服务   1.在可以联网的机器上使用yum工具安装,如果未联网,则挂载系统光盘进行安装. # yum install samba samba-client samba-swat 有依赖关 ...

  10. python API _ 1 (EasyDict)

    作用:参数调用文件一:from easydict import EasyDict as edictimport numpy as np config = edict() config.IMG_HEIG ...