参考资料:

  1. https://msdn.microsoft.com/en-us/library/system.threading.manualresetevent.aspx

  2. https://msdn.microsoft.com/en-us/library/system.threading.autoresetevent.aspx

  二者的区别,跑一下下边的程序就懂了。

  ManualResetEvent:通知一个或多个等待线程该事件已经发生。

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks; namespace Test01
{
class Program
{
private static ManualResetEvent mre = new ManualResetEvent(false); static void Main(string[] args)
{
for (int i = ; i < ; i++)
{
Thread thread = new Thread(ThreadProc);
thread.Name = "Thread_" + i;
thread.Start();
} Thread.Sleep();
Console.WriteLine("If you want to release all threads. Please press 'Enter'.");
Console.ReadLine(); mre.Set();
Console.WriteLine("All threads have been released.");
mre.Reset();
} private static void ThreadProc()
{
string threadName = Thread.CurrentThread.Name; Console.WriteLine("Thread: " + threadName + " starts and begin WaitOne().");
mre.WaitOne();
Console.WriteLine("Thread: " + threadName + " gets the signal and ends.");
}
}
}

ManualResetEvent

  AutoResetEvent:通知一个等待线程该事件已经发生。

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks; namespace Test02
{
class Program
{
private static AutoResetEvent are = new AutoResetEvent(false); static void Main(string[] args)
{
for (int i = ; i < ; i++)
{
Thread thread = new Thread(ThreadProc);
thread.Name = "Thread_" + i;
thread.Start();
} Thread.Sleep(); Console.WriteLine("Please press Enter to release the signal.");
Console.ReadLine();
are.Set();
} private static void ThreadProc()
{
string threadName = Thread.CurrentThread.Name; Console.WriteLine("Thread: " + threadName + " waits for AutoResetEvent.");
are.WaitOne();
Console.WriteLine("Thread: " + threadName + " gets the signal and is released.");
are.Set();
}
}
}

AutoResetEvent_1

ManualResetEvent & AutoResetEvent的更多相关文章

  1. 重新想象 Windows 8 Store Apps (47) - 多线程之线程同步: Semaphore, CountdownEvent, Barrier, ManualResetEvent, AutoResetEvent

    [源码下载] 重新想象 Windows 8 Store Apps (47) - 多线程之线程同步: Semaphore, CountdownEvent, Barrier, ManualResetEve ...

  2. ManualResetEvent,AutoResetEvent说明

    相信不少人对ManualResetEvent,AutoResetEvent的状态比较晕,下面是本人认为最精简的理解 1.只有2种状态,终止态 And 非终止态 终止状态,既然是状态那么一定对应事物,这 ...

  3. AutoResetEvent和ManualResetEvent理解 z

    AutoResetEvent和ManualResetEvent用于多线程之间代码执行顺序的控制,它们继承自WaitHandle,API相同,但在使用中还是有区别的. 每次使用时虽然理解了,但由于没有去 ...

  4. AutoResetEvent和ManualResetEvent理解

    AutoResetEvent和ManualResetEvent用于多线程之间代码执行顺序的控制,它们继承自WaitHandle,API相同,但在使用中还是有区别的. 每次使用时虽然理解了,但由于没有去 ...

  5. C# Event 内核构造 |EventWaitHandle、AutoResetEvent、 ManualResetEvent

    EventWaitHandle 继承:Object->WaitHandle-> EventWaitHandle派生:System.Threading.AutoResetEvent\Syst ...

  6. C# 同步转异步 AutoResetEvent

    当我们的程序运行时,调用了一段异步的逻辑A,这段异步的逻辑无法转化为同步(如动画.下载进度等) 而,我们又需要等待异步逻辑A处理完成,然后再执行其它逻辑B. AutoResetEvent 同步转异步 ...

  7. C#与C++的发展历程第三 - C#5.0异步编程巅峰

    系列文章目录 1. C#与C++的发展历程第一 - 由C#3.0起 2. C#与C++的发展历程第二 - C#4.0再接再厉 3. C#与C++的发展历程第三 - C#5.0异步编程的巅峰 C#5.0 ...

  8. C#并发编程

    并发编程,一直是小白变成(●—●)的一个坎.平时也用到过不少并发编程操作,在这里进行一下记录. 多线程并不是唯一 并发:同时做多件事情. 多线程:并发的一种形式,采用多线程来执行程序. 并行处理:把正 ...

  9. C#多线程操作界面控件的解决方案(转)

    C#中利用委托实现多线程跨线程操作 - 张小鱼 2010-10-22 08:38 在使用VS2005的时候,如果你从非创建这个控件的线程中访问这个控件或者操作这个控件的话就会抛出这个异常.这是微软为了 ...

随机推荐

  1. db.properties

    jdbc.driverclass=oracle.jdbc.driver.OracleDriverjdbc.url=jdbc:oracle:thin:@192.168.201.192:1521:orcl ...

  2. seo之google rich-snippets丰富网页摘要结构化数据(微数据)实例代码

    seo之google rich-snippets丰富网页摘要结构化数据(微数据)实例代码 网页摘要是搜索引擎搜索结果下的几行字,用户能通过网页摘要迅速了解到网页的大概内容,传统的摘要是纯文字摘要,而结 ...

  3. Web SQL

    HTML5本地存储——Web SQL Database 基于 HTML5 中的 Web SQL Database 来构建应用程序 前端HTML5几种存储方式的总结 <!DOCTYPE HTML& ...

  4. grep、egrep、fgrep

    grep: global search regular expression and printing

  5. Hadoop集群管理之配置文件

    一.配置文件列表如下: [hadoop@node1 conf]$ pwd /app/hadoop/conf [hadoop@node1 conf]$ echo $HADOOP_HOME /app/ha ...

  6. phpize的深入理解

    安装(fastcgi模式)的时候,常常有这样一句命令:/usr/local/webserver/php/bin/phpize一.phpize是干嘛的?phpize是什么东西呢?php官方的说明:htt ...

  7. [HTML]网页开发学习笔记

    为了要开发一套教学使用的教师管理系统,(客户需求使用网页做教师控制端口)我便学习了一下HTML网页开发. 很不错的学习开发的网站:http://www.w3school.com.cn/index.ht ...

  8. poj1961 Period

    我们考虑KMP算法中fail失配指针的意义. 对于一个模式串(Pattern),位置i对应的失配指针fail[i]是那个位置: 这个位置满足的条件是,子串[0, fail[i])是位置i(不含)的后缀 ...

  9. Unity-Animator深入系列---FAQ

    回到 Animator深入系列总目录 Q: 如果想做角色负伤的一套动画,但是又和原有状态机不冲突,只是想换动画剪辑,应该怎么办? A: 新建一个层,设置为同步模式.这时候你不能在同步层添加状态,但你可 ...

  10. Uva 1347,旅行

    题目链接:https://uva.onlinejudge.org/external/13/1347.pdf 这个题和uva 1658题目很像,只是加了一点,就是每个点都要走,刚开始,我以为可以直接拆点 ...