当前位置:网站首页>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.");
}
}边栏推荐
- Small guide for rapid completion of mechanical arm (VI): stepping motor driver
- OpenGL ES: (1) OpenGL ES的由来 (转)
- OpenGL ES: (2) OpenGL ES 与 EGL、GLSL的关系
- 69 Cesium代码datasource加载geojson
- CJC8988带2个立体声耳机驱动器的低功率立体声编解码器
- What if the data in the cloud disk is harmonious?
- OpenGL es: (5) basic concepts of OpenGL, the process of OpenGL es generating pictures on the screen, and OpenGL pipeline
- Multi label lsml for essay learning records
- OpenGL ES: (3) EGL、EGL绘图的基本步骤、EGLSurface、ANativeWindow
- three.js小结
猜你喜欢

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

Timer based on LabVIEW

OpenGL ES: (5) OpenGL的基本概念、OpenGL ES 在屏幕产生图片的过程、OpenGL管线(pipeline)

Diagramme dynamique Excel

excel動態圖錶

π disk, turning your computer into a personal private cloud

68 Cesium代码datasource加载czml

skywalking集成nacos动态配置

Index method and random forest to realize the information of surface water body in wet season in Shandong Province

3D打印机穿线:5种简单的解决方案
随机推荐
无限水平大理石游戏
浏览器端保存数据到本地文件
Make Tiantou village sweet. Is Xianjing taro or cabbage the characteristic agricultural product of Tiantou Village
DEV XPO对比之UOW
解决麒麟V10上传文件乱码问题
SystemVerilog学习-06-类的封装
skywalking集成nacos动态配置
MySQL里记录货币
论文学习记录随笔 多标签之LSML
Seven major technical updates that developers should pay most attention to on build 2022
论文学习记录随笔 多标签之GLOCAL
restframework-simpleJWT重写认证机制
OpenGL es: (2) relationship between OpenGL es, EGL and glsl
OpenGL es: (5) basic concepts of OpenGL, the process of OpenGL es generating pictures on the screen, and OpenGL pipeline
Database problems, how to optimize Oracle SQL query statements faster and more efficient
SystemVerilog学习-08-随机约束和线程控制
Linux closes the redis process SYSTEMd+
Save data in browser to local file
Freeswitch dial the extension number
three. JS summary