ManualResetEvent在线程里简单应用
来源:互联网 发布:手绘软件下载mac 编辑:程序博客网 时间:2024/06/03 14:38
在C#开发过程中,需要用线程来处理比较复杂的逻辑,用Thread自身的相关函数比如Suspend(),Resume()等函数有时不能满足需求。使用ManualResetEvent比较简单实现Suspend(),Resume()等函数的功能。
实现代码大体如下:
Thread thread = null;//线程的声明
ManualResetEvent manualEvent = new ManualResetEvent(true); //定义 初始状态为true,说明一开始有信号
//线程运行的处理函数
public void Run()
{
int cur_id = 0;
for (int id = 0; id < Id_list.Count; id++)
{
manualEvent.WaitOne();
//...逻辑的处理
Thread.Sleep(1000);
}
}
//启动一个线程
thread = new Thread(new ThreadStart(Run));
thread.Start();
//暂停一个线程的运行 Event没有信号,WaitOne()一直在等。
manualEvent.Reset();
//重让线程运行 Event有信号,WaitOne()收到信号向下执行。
manualEvent.Set();
//结束线程
if (thread != null)
thread.Abort();
至此ManualResetEvent在线程中简单应用完成。
参考:
https://msdn.microsoft.com/zh-cn/library/system.threading.manualresetevent.aspx
- ManualResetEvent在线程里简单应用
- 线程 ManualResetEvent 类
- 线程间通信 ManualResetEvent
- 线程同步 ManualResetEvent
- ManualResetEvent 线程同步
- 线程 ManualResetEvent 类
- 线程同步 - EventWaitHandle & AutoResetEvent & ManualResetEvent
- ManualResetEvent,AutoResetEvent类线程同步
- AutoResetEvent和ManualResetEvent 线程同步
- 线程同步:ManualResetEvent和AutoResetEvent
- 正则表达式在asp里的简单应用,验证手机号码
- ManualResetEvent
- ManualResetEvent
- ManualResetEvent
- ManualResetEvent
- ManualResetEvent
- ManualResetEvent
- 线程池在web上的简单应用
- 五大常用算法
- (原) 高德地图
- swift - tableView的fream, contentInset, contentOffset, contentSize等属性
- linux一些指令的补充
- angularjs之directive实例
- ManualResetEvent在线程里简单应用
- STL sort的使用
- Http协议
- 动态按钮,滑动条
- perl 调用rm -rf 递归删除指定文件夹,提示 can not remove: not empty 原因
- Android开发中单例模式写法与可能遇到的坑
- Swift - 贝赛尔曲线
- MFC中的小问题,求教!!!!
- 8051单片机的I/O口