参考资料:

  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. 【Pro ASP.NET MVC 3 Framework】.学习笔记.8.SportsStore:管理

    管理功能,如何身份认证,对controller和action方法过滤安全的访问,并在用户需要时提供证书. 1 添加分类管理 方便管理的controller,有两类页面,List页面和edit页面. 1 ...

  2. 新学习的语言Groovy

    什么是 Groovy? Groovy 是 JVM 的一个替代语言 —替代 是指可以用 Groovy 在 Java 平台上进行 Java 编程,使用方式基本与使用 Java 代码的方式相同.在编写新应用 ...

  3. http协议了解

    在web应用中,服务器把网页的HTML代码发送给浏览器,让浏览器显示出来,浏览器和服务器之间的传输协议就是HTTP协议.HTTP是在网络上传输HTML的协议,用于浏览器和服务器之间的通信. 一个网页打 ...

  4. python: hashlib 加密模块

    加密模块hashlib import hashlib m=hashlib.md5() m.update(b'hello') print(m.hexdigest()) #十六进制加密 m.update( ...

  5. HDU 4712:Hamming Distance

    Hamming Distance Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) ...

  6. SlickGrid example 6:Ajax加载

    Ajax加载.   代码:   <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Ty ...

  7. SlickGrid example 3b: 支持撤销操作的编辑单元

    不同类型的属性可以按不同的风格编辑. 每个编辑单元可以设置不同的验证方法. 历史编辑可以撤销.   代码: <!DOCTYPE html PUBLIC "-//W3C//DTD HTM ...

  8. SQL Server 索引和表体系结构(三)

    转自:http://www.cnblogs.com/chenmh/p/3785285.html 包含列索引 概述 包含列索引也是非聚集索引,索引结构跟聚集索引结构是一样,有一点不同的地方就是包含列索引 ...

  9. Watch The Movie

    Watch The Movie Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) Tot ...

  10. SortedDictionary和SortedList

    使用上两者的接口都类似字典,并且SortedList的比如Find,FindIndex,RemoveAll常用方法都没提供. 数据结构上二者差异比较大,SortedList查找数据极快,但添加新元素, ...