-
C#教程之C# ManualResetEvent用法详解
本站最新发布 C#从入门到精通
试听地址 https://www.xin3721.com/eschool/CSharpxin3721/
试听地址 https://www.xin3721.com/eschool/CSharpxin3721/
ManualResetEvent表示线程同步事件,可以对所有进行等待的线程进行统一管理(收到信号时必须手动重置该事件)
其构造函数为:
1
|
public ManualResetEvent ( bool initialState); |
参数 initialState 表示是否初始化,如果为 true,则将初始状态设置为终止(不阻塞);如果为 false,则将初始状态设置为非终止(阻塞)。
注意:如果其参数设置为true,则ManualResetEvent等待的线程不会阻塞。 如果初始状态为false, 则在Set调用方法之前, 将阻止线程。
它只有两个方法
1
2
3
4
5
6
7
|
//将事件状态设置为终止状态,从而允许继续执行一个或多个等待线程。 public bool Set (); //将事件状态设置为非终止,从而导致线程受阻。 public bool Reset (); //返回值:操作成功返回true,否则false |
讲了这么多,看个例子理解一下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
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以释放最后一个线程, 程序结束。
如果还不是很清楚可以进行单步调试,这样就能明白了!
文章参考MSDN:ManualResetEvent Class
栏目列表
最新更新
C# 面向对象
假设客车的座位数是9行4列,使用二维数
C#基于接口设计三层架构Unity篇
C#线程 入门
C#读取静态类常量属性和值
C# 插件式编程
C# 委托与事件有啥区别?
C#队列学习笔记:队列(Queue)和堆栈(Stack
linq 多表分组左连接查询查询统计
C#队列学习笔记:MSMQ入门一
C# 在Word中添加Latex 数学公式和符号
inncheck命令 – 检查语法
基于UDP的服务器端和客户端
再谈UDP和TCP
在socket编程中使用域名
网络数据传输时的大小端问题
socket编程实现文件传输功能
如何优雅地断开TCP连接?
图解TCP四次握手断开连接
详细分析TCP数据的传输过程
SqlServer 利用游标批量更新数据
BOS只读状态修改
SQL Server等待事件—PAGEIOLATCH_EX
数据库多行转换为单一列
获取数据表最后最后访问,修改,更新,
计算经历的时间
SQL查询结果自定义排序
修改数据库默认位置
日期简单加或减
从日期获取年,月或日