ManualResetEvent & AutoResetEvent
参考资料:
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的更多相关文章
- 重新想象 Windows 8 Store Apps (47) - 多线程之线程同步: Semaphore, CountdownEvent, Barrier, ManualResetEvent, AutoResetEvent
[源码下载] 重新想象 Windows 8 Store Apps (47) - 多线程之线程同步: Semaphore, CountdownEvent, Barrier, ManualResetEve ...
- ManualResetEvent,AutoResetEvent说明
相信不少人对ManualResetEvent,AutoResetEvent的状态比较晕,下面是本人认为最精简的理解 1.只有2种状态,终止态 And 非终止态 终止状态,既然是状态那么一定对应事物,这 ...
- AutoResetEvent和ManualResetEvent理解 z
AutoResetEvent和ManualResetEvent用于多线程之间代码执行顺序的控制,它们继承自WaitHandle,API相同,但在使用中还是有区别的. 每次使用时虽然理解了,但由于没有去 ...
- AutoResetEvent和ManualResetEvent理解
AutoResetEvent和ManualResetEvent用于多线程之间代码执行顺序的控制,它们继承自WaitHandle,API相同,但在使用中还是有区别的. 每次使用时虽然理解了,但由于没有去 ...
- C# Event 内核构造 |EventWaitHandle、AutoResetEvent、 ManualResetEvent
EventWaitHandle 继承:Object->WaitHandle-> EventWaitHandle派生:System.Threading.AutoResetEvent\Syst ...
- C# 同步转异步 AutoResetEvent
当我们的程序运行时,调用了一段异步的逻辑A,这段异步的逻辑无法转化为同步(如动画.下载进度等) 而,我们又需要等待异步逻辑A处理完成,然后再执行其它逻辑B. AutoResetEvent 同步转异步 ...
- 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 ...
- C#并发编程
并发编程,一直是小白变成(●—●)的一个坎.平时也用到过不少并发编程操作,在这里进行一下记录. 多线程并不是唯一 并发:同时做多件事情. 多线程:并发的一种形式,采用多线程来执行程序. 并行处理:把正 ...
- C#多线程操作界面控件的解决方案(转)
C#中利用委托实现多线程跨线程操作 - 张小鱼 2010-10-22 08:38 在使用VS2005的时候,如果你从非创建这个控件的线程中访问这个控件或者操作这个控件的话就会抛出这个异常.这是微软为了 ...
随机推荐
- 【secureCRT】如何在secureCRT上设置常用的快捷输出按钮栏
转自:http://jingyan.baidu.com/article/5d6edee2f32de199eadeec25.html 方法/步骤 打开secureCRT软件,单击菜单栏上面的[查看] ...
- 好用的SSH客户端 good SSH client recommended
对于经常性地要登录服务器的同志们,选择一款优秀的SSH客户端非常有必要,不仅可以提高效率,而且赏心悦目,宅的几率更大.呵呵,我就是i一枚.很出名的就是PuTTY(Windows,Linux都有的), ...
- Linux终端乱码的解决办法
用SSH连接Linux时经常会遇到乱码的情况,痛苦了好久,在网上找到一个解决办法,编辑~/.bash_profile文件,加入下面两行: LANG="zh_CN.GB18030" ...
- golang的helloworld
新建源码文件hello.go mkdir -p /work/goTest/ cd /work/goTest/ vim hello.go 编码hello.go文件: package main impor ...
- PHP程序中删除字符串最后一个字符的三种方法
常见的语法格式: foreach ($arr as $key => $value) {$arr_str = $arr['x_id'] . ',' . $arr_str;} 假设字符数组 $arr ...
- oracle 存储过程和函数例子
关于 游标 if,for 的例子 create or replace procedure peace_if is cursor var_c is select * from grade; begin ...
- mysqldump备份过程中都干了些什么
mysqldump备份方便,易读,功能丰富,相信大家都有 使用过这个命令进行备份,但是这个命令在备份的过程中都做了写什么呢,下面打开general_log进行查看: 1.登录mysql命令行客户端: ...
- Ombrophobic Bovines 分类: POJ 图论 最短路 查找 2015-08-10 20:32 2人阅读 评论(0) 收藏
Ombrophobic Bovines Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16539 Accepted: 3605 ...
- Parencodings 分类: POJ 2015-06-28 22:00 7人阅读 评论(0) 收藏
Parencodings Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22757 Accepted: 13337 De ...
- C#中八皇后问题的递归解法——N皇后
百度测试部2015年10月份的面试题之——八皇后. 八皇后问题的介绍在此.以下是用递归思想实现八皇后-N皇后. 代码如下: using System;using System.Collections. ...