当前位置:网站首页>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.");
}
}边栏推荐
- What if the data in the cloud disk is harmonious?
- Freeswitch dial the extension number
- 无限水平大理石游戏
- linux 关闭redis 进程 systemd+
- 让厦门灌口镇田头村变甜头村的特色农产品之一是蚂蚁新村
- 穿越派 你的数据云行
- 2022 年面向初学者的 10 大免费 3D 建模软件
- PLA不粘貼在床上:6個簡單的解决方案
- Leetcode Max rectangle, Max square series 84 85. 221. 1277. 1725. (monotonic stack, dynamic programming)
- 亲爱的派盘用户,我要向您表白!
猜你喜欢

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

千万不要把笔记视频乱放!
![Pit of kotlin bit operation (bytes[i] and 0xff error)](/img/2c/de0608c29d8af558f6f8dab4eb7fd8.png)
Pit of kotlin bit operation (bytes[i] and 0xff error)

DHT11 温湿度传感器

可动的机械挂钟

Primary application case of Excel DuPont analyzer

freeswitch拨打分机号

Huluer app help

Small guide for rapid completion of mechanical arm (VI): stepping motor driver

three.js小结
随机推荐
Chip, an empire built on sand!
Freeswitch dial the extension number
Some errors encountered in MySQL data migration
Leetcode Max rectangle, Max square series 84 85. 221. 1277. 1725. (monotonic stack, dynamic programming)
OpenGL ES: (5) OpenGL的基本概念、OpenGL ES 在屏幕产生图片的过程、OpenGL管线(pipeline)
Why use huluer pie disk instead of U disk?
1034 Head of a Gang
Transformer le village de tiantou en un village de betteraves sucrières
The row and column numbers of each pixel of multi-source grid data in the same area are the same, that is, the number of rows and columns are the same, and the pixel size is the same
Huluer app help
Advanced drawing skills of Excel lecture 100 (1) - use Gantt chart to show the progress of the project
Primary application case of Excel DuPont analyzer
Record currency in MySQL
3D printer threading: five simple solutions
excel动态图表
ArcServer密码重置(账号不可以重置)
穿越派与贸大合作,为大学生增添效率
SystemVerilog学习-06-类的封装
69 cesium code datasource loading geojson
SQL必会题之留存率