当前位置:网站首页>C# ManualResetEvent 类的理解
C# ManualResetEvent 类的理解
2022-07-01 06:06:00 【旧时量刑】
ManualResetEvent 类
它可以通知一个或多个正在等待的线程已发生事件,允许线程通过发信号互相通信,来控制线程是否可以访问资源
常用语法含义:
- ManualResetEvent manualEvent = new ManualResetEvent(true);//为true(signaled状态),一开始就可以执行,线程不能被阻塞
- ManualResetEvent manualEvent = new ManualResetEvent(false);//为flase(nonsignaled可阻塞状态),一开始就不可以执行
- manualEvent.Reset(); //将事件状态设置为非终止状态,导致线程阻止。
- manualEvent.Set(); //将事件状态设置为终止状态,允许一个或多个等待线程继续。
- manualEvent.WaitOne ();阻塞当前线程直到别的线程调用Set方法,只有ManualResetEvent 的状态是可阻塞(nonsignaled)时,WaitOne才能真正的起到作用;

//来自于 C# ManualResetEvent 类的用法_随风吹笛的博客-CSDN博客_manualresetevent
internal class Program
{
private static ManualResetEvent mre = new ManualResetEvent(false);
static void Main(string[] args)
{
Console.WriteLine("启动在ManualResetEvent上阻塞的3个线程");
for (int i = 0; i <= 2; i++)
{
Thread t = new Thread(ThreadProc);
t.Name = "Thread_" + i;
t.Start();
}
Thread.Sleep(500);
Console.WriteLine("当所有三个线程都启动后,按Enter键调用Set ()去释放所有线程。");
Console.ReadLine();
mre.Set();
Thread.Sleep(500);
Console.WriteLine("当ManualResetEvent被发出信号时,调用WaitOne()的线程不会阻塞。按回车键来显示这个");
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("按Enter键调用Reset(),这样线程在调用WaitOne()时就会再次阻塞。");
Console.ReadLine();
mre.Reset();
// 启动一个等待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();
Console.ReadLine();
}
private static void ThreadProc()
{
string name = Thread.CurrentThread.Name;
Console.WriteLine(name + " 启动并调用mre.WaitOne()");
mre.WaitOne();
Console.WriteLine(name + " ends.");
}
}边栏推荐
- 数据库er图组成要素
- 讓田頭村變甜頭村的特色農產品是仙景芋還是白菜
- Differences between in and exists in MySQL
- Fragment upload and breakpoint resume
- srpingboot security demo
- What if the data in the cloud disk is harmonious?
- jdbc 数据库操作
- In win10 and win11, the scroll direction of Elan touch panel is reversed, and "double finger click to open the right-click menu" and "double finger scroll" are started“
- Ant new village is one of the special agricultural products that make Tiantou village in Guankou Town, Xiamen become Tiantou village
- Infinite horizontal marble game
猜你喜欢

Excel dynamic chart

4GB大文件,如何实时远程传输和共享?

Preliminary level of C language -- selected good questions on niuke.com

Crossing sect · paipan + Siyuan notes = private notebook

Diagramme dynamique Excel

excel动态图表

Advanced drawing skills of Excel lecture 100 (1) - use Gantt chart to show the progress of the project

excel初级应用案例——杜邦分析仪

论文学习记录随笔 多标签之LSML

uniapp树形层级选择器
随机推荐
Multi label lsml for essay learning records
Enter an expression (expressed as a string) and find the value of this expression.
SystemVerilog学习-06-类的封装
Highmap gejson data format conversion script
Dear pie users, I want to confess to you!
freeswitch拨打分机号
Arcserver password reset (account cannot be reset)
How to transmit and share 4GB large files remotely in real time?
Preliminary level of C language -- selected good questions on niuke.com
数据库问题,如何优化Oracle SQL查询语句更快,效率更高
π盘,让您电脑变成个人的私有云
DHT11 temperature and humidity sensor
Thesis learning record essay multi label lift
restframework-simpleJWT重写认证机制
linux 关闭redis 进程 systemd+
相同区域 多源栅格数据 各个像元行列号一致,即行数列数相同,像元大小相同
Debug details under pycharm
Oracle 序列+触发器
Know the future of "edge computing" from the Nobel prize!
穿越派 你的数据云行