C# ManualResetEvent使用方法详解
作者:田志良 时间:2022-05-15 23:57:51
本文实例为大家分享了ManualResetEvent的使用方法,供大家参考,具体内容如下
1. 源码下载:
下载地址:ManualResetEvent
Demo:
2. ManualResetEvent详解
ManualResetEvent 允许线程通过发信号互相通信。通常,此通信涉及一个线程在其他线程进行之前必须完成的任务。当一个线程开始一个活动(此活动必须完成后,其他线程才能开始)时,它调用 Reset 以将 ManualResetEvent 置于非终止状态,此线程可被视为控制 ManualResetEvent。调用 ManualResetEvent 上的 WaitOne 的线程将阻止,并等待信号。当控制线程完成活动时,它调用 Set 以发出等待线程可以继续进行的信号。并释放所有等待线程。一旦它被终止,ManualResetEvent 将保持终止状态(即对 WaitOne 的调用的线程将立即返回,并不阻塞),直到它被手动重置。可以通过将布尔值传递给构造函数来控制 ManualResetEvent 的初始状态,如果初始状态处于终止状态,为 true;否则为 false。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace ManualResetEventDemo
{
class MREDemo
{
private ManualResetEvent _mre;
public MREDemo()
{
this._mre = new ManualResetEvent(true);
}
public void CreateThreads()
{
Thread t1 = new Thread(new ThreadStart(Run));
t1.Start();
Thread t2 = new Thread(new ThreadStart(Run));
t2.Start();
}
public void Set()
{
this._mre.Set();
}
public void Reset()
{
this._mre.Reset();
}
private void Run()
{
string strThreadID = string.Empty;
try
{
while (true)
{
// 阻塞当前线程
this._mre.WaitOne();
strThreadID = Thread.CurrentThread.ManagedThreadId.ToString();
Console.WriteLine("Thread(" + strThreadID + ") is running...");
Thread.Sleep(5000);
}
}
catch(Exception ex)
{
Console.WriteLine("线程(" + strThreadID + ")发生异常!错误描述:" + ex.Message.ToString());
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ManualResetEventDemo
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("****************************");
Console.WriteLine("输入\"stop\"停止线程运行...");
Console.WriteLine("输入\"run\"开启线程运行...");
Console.WriteLine("****************************\r\n");
MREDemo objMRE = new MREDemo();
objMRE.CreateThreads();
while (true)
{
string input = Console.ReadLine();
if (input.Trim().ToLower() == "stop")
{
Console.WriteLine("线程已停止运行...");
objMRE.Reset();
}
else if (input.Trim().ToLower() == "run")
{
Console.WriteLine("线程开启运行...");
objMRE.Set();
}
}
}
}
}
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C# 大数据导出word的假死报错的处理方法
Android中使用自定义ViewGroup的总结
![](https://img.aspxhome.com/file/2023/0/116050_0s.jpg)
Android 友盟第三方登录与分享的实现代码
![](https://img.aspxhome.com/file/2023/6/114376_0s.png)
Spring Data JPA中 in 条件参数的传递方式
JavaWeb开发之【Tomcat 环境配置】MyEclipse+IDEA配置教程
![](https://img.aspxhome.com/file/2023/8/85478_0s.png)
springboot使用之多个filter的执行顺序以及配置方式
![](https://img.aspxhome.com/file/2023/5/59045_0s.jpg)
使用纯Java实现一个WebSSH项目的示例代码
![](https://img.aspxhome.com/file/2023/8/64148_0s.png)
unity 如何使用LineRenderer 动态划线
![](https://img.aspxhome.com/file/2023/9/102269_0s.jpg)
如何用Java注解和反射实现依赖注入
C# ArrayList、HashSet、HashTable、List、Dictionary的区别详解
Java算法之递归算法计算阶乘
![](https://img.aspxhome.com/file/2023/4/63454_0s.png)
Android带清除功能的输入框控件EditTextWithDel
![](https://img.aspxhome.com/file/2023/3/91013_0s.gif)
编写android拨打电话apk应用实例代码
详解Maven多模块打包遇到的问题解决方法
![](https://img.aspxhome.com/file/2023/2/72652_0s.png)
java控制台实现学生信息管理系统(集合版)
![](https://img.aspxhome.com/file/2023/2/77162_0s.jpg)
Java动态代理四种实现方式详解
![](https://img.aspxhome.com/file/2023/8/94198_0s.png)
Java控制台实现猜拳游戏
全面总结java IO体系
![](https://img.aspxhome.com/file/2023/3/73093_0s.jpg)
C#实现快速排序算法
![](https://img.aspxhome.com/file/2023/8/84478_0s.jpg)