ManualResetEvent用法

ManualResetEvent 允许线程通过发信号互相通信。通常,此通信涉及一个线程在其他线程进行之前必须完成的任务。

public static ManualResetEvent mre = new ManualResetEvent(false);
ManualResetEvent建立时是把false作为start的初始状态,这个类用于通知另一个线程,让它等待一个或多个线程。注意,为了通知或监听同一个线程,所有的其它线程都能访问那个类。
等待线程这样写:
  mre.WaitOne();
这将引起等待线程无限期的阻塞并等待类来通知。
发信号的线程应该这样:
  mre.Set();
这样类就会被通知,值变成true,等待线程就会停止等待。在通知事件发生后,我们就可以使用下面语句把线程置于非终止状态,导致线程阻止:
  mre.Reset();
一个测试的例子:

using System;
using System.Threading;
namespace ThreadingTester
{
    
class ThreadClass
    
{
        
public static ManualResetEvent mre = new ManualResetEvent(false);
        
public static void trmain()
        
{
            Thread tr 
= Thread.CurrentThread;
            Console.WriteLine(
"thread: waiting for an event");
            mre.WaitOne();
            Console.WriteLine(
"thread: got an event");
            
for (int x = 0; x < 10; x++)
            
{
                Thread.Sleep(
1000);
                mre.WaitOne();
                Console.WriteLine(tr.Name 
+ "" + x);
            }

        }

        
static void Main(string[] args)
        
{
            Thread thrd1 
= new Thread(new ThreadStart(trmain));
            thrd1.Name 
= "thread1";
            thrd1.Start();
            
for (int x = 0; x < 10; x++)
            
{
                Thread.Sleep(
900);
                Console.WriteLine(
"Main:" + x);
                
if (5 == x) mre.Set();
                
if (6 == x) mre.Reset();
                
if (8 == x) mre.Set();
            }

            
while (thrd1.IsAlive)
            
{
                Thread.Sleep(
1000);
                Console.WriteLine(
"Main: waiting for thread to stop");
            }

        }

    }

}

运行的结果为:
thread: waiting for an event
Main:0
Main:1
Main:2
Main:3
Main:4
Main:5
thread: got an event
Main:6
Main:7
Main:8
thread1: 0
Main:9
thread1: 1
Main: waiting for thread to stop
thread1: 2
Main: waiting for thread to stop
thread1: 3
Main: waiting for thread to stop
thread1: 4
Main: waiting for thread to stop
thread1: 5
Main: waiting for thread to stop
thread1: 6
Main: waiting for thread to stop
thread1: 7
Main: waiting for thread to stop
thread1: 8
Main: waiting for thread to stop
thread1: 9
Main: waiting for thread to stop

转载于:https://www.cnblogs.com/fengfeng/archive/2008/06/24/1229037.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/381629.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

2014_anshan_onsite

5070 Twelve Months 5071 Chat 比较长的模拟 5072 Coprime 容斥 同色三角形建模后&#xff0c;转化为互质个数问题 5073 Galaxy 数学题&#xff0c;公式递推和化简 5074 Hatsune Miku dp,分类讨论和递推 5075 Random Inversion Ma…

几种流行的JS框架的选择

目前来看&#xff0c;JS框架以及一些开发包和库类有如下几个&#xff0c;Dojo 、Scriptaculous 、Prototype 、yui-ext 、Jquery 、Mochikit、mootools 、moo.fxAjaxian在2007年底对Ajax工具进行了调查&#xff0c;部分调查结果见下表&#xff08;其中数字为调查者使用该工具的…

2014_beijing_onsite

5112 A Curious Matt 签到&#xff0c;排序题目 5113 Black And White 5*5的方格&#xff0c;dfs剪枝 5114 Collision 线性方程求解 扩展欧几里得 5115 Dire Wolf 简单的区间DP问题。 5116 Everlasting L 5117 Fluorescent …

[转]关于HTTP服务器每个客户端2个连接的限制

这两天猫在家里搞一个多线程的断点续传得C#程序&#xff0c;发现同时只能开2个线程下载&#xff0c;其他的线程一律要等待&#xff0c;这样就导致下载大文件时其他线程经常超时&#xff0c;郁闷好久。今天回公司无意中发现了一个帖子&#xff0c;终于真相大白了&#xff0c; …

2014_guangzhou_onsite

5127 Dogs Candies 链表&#xff0c;暴力 5128 The E-pang Palace 计算几何水题&#xff0c;求不相交两个矩形的最大面积 5129 Yong Zhengs Death 5130 Signal Interference 计算圆与多边形的交&#xff0c;计算几何模板题目 5131 Song Jian…

[ SAP ]MM Valuation System

1、除了工厂&#xff0c;另一个会决定不同的会计科目的因素是物料本身&#xff0c;所以我们在物料主数据中会需要定义一个“评估类”参数&#xff0c;通过“评估类”来决定会计科目。 2、创建/更改/删除物料主数据&#xff1a;后勤-》物料管理-》物料主数据-》物料 MM01创建物…

2014_shanghai_visit

5090 Game with Pearls 签到题目&#xff0c;直接模拟即可。 5091 Beam Cannon 矩形覆盖的最大点数&#xff0c;离散化扫描线线段树。 5092 Seam Carving 记录路径的DAG 5093 Battle ships 行列建边&#xff0c;二分图匹配 5094 Maze …

从MySQL导入导出大量数据的程序实现方法

大家一定使用过 phpmyadmin 里面的数据库导入&#xff0c;导出功能&#xff0c;非常方便。但是在实际应用中&#xff0c;我发现如下几个问题&#xff1a; 1 数据库超过一定尺寸&#xff0c;比如6M 这时使用导出一般没问题&#xff0c;可以正确的保存到本机硬盘上面&#xff0c…

2013_chengdu_visit

4716 A Computer Graphics Problem 签到题目&#xff0c;模拟。 4717 The Moving Points 求n个点的最大距离最小值&#xff0c;三分时间即可 4718 The LCIS on the Tree LCT动态树 4719 Oh My Holy FFF 4720 Naive and Silly M…

JavaScript: Cookie 详解、实例与应用

Cookie&#xff08;也&#xff09;是JavaScript中的一种机制&#xff0c;可以实现严格的跨页面全局变量的要求。 Cookie是存于用户硬盘的一个文件&#xff0c;这个文件通常对应于一个域名&#xff0c;当浏览器再次访问这个域名时&#xff0c;便使这个cookie可用。因此&#xff…

2013_warmup

感觉题目质量很差的一套&#xff0c;可能是不正式的原因。 4706 Childrens Day 签到题。 4707 Pet 签到题目&#xff0c;BFS或者DFS; 4708 Rotation Lock Puzzle 分析模拟题 4709 Herding 计算几何水题。 4710 Balls R…

cs模式下,显示网络图片一例

由于在cs模式中 显示图片很简单picturebox1.imageimage.fromfile(filename) 但是filename不能是url&#xff0c;所以不能显示网络图片&#xff0c;怎么办呢&#xff1f;其实 .net 为我们准备了一套很方便的方法。在.net中&#xff0c;网上的资源&#xff08;图片&#xff0c;动…

2013_chengdu_online

4728 A Game in the Hospital 4729 An Easy Problem for Elfness 4730 We Love MOE Girls 签到题目 4731 Minimum palindrome 规律构造 4732 Round Table 4733 G(x) 枚举dp 4734 F(x) 数位dp基础题目。…

【Vegas2008】7月19日-凉粉的做法

1&#xff09; 准备淀粉&#xff0c;1个量杯。2&#xff09; 盛出1杯淀粉到小锅里&#xff0c;倒6份凉水到小锅里。淀粉和水的比例是1&#xff1a;6&#xff0c;用什么工具来量并不重要&#xff0c;把比例弄好了就行。另外&#xff0c;关于比例&#xff0c;有人喜欢1&#xff1…

2013_hangzhou_online

4738 Caocaos Bridges 求无向图的桥中最小的那个&#xff0c;tarjan 4739 Zhuge Liangs Mines 状态压缩暴力 4740 The Donkey of Gui Zhou 求两个点在图上的相遇点&#xff0c;模拟dfs 4741 Save Labman No.004 计算几何&#xff0c;平面上的两条…

骑行封龙山

最近受不了luoluo和尚的鄙视加刺激(车子比我晚买好几个月&#xff0c;骑行路程快1000公里了-_-),中午心血来潮就随便选了个路线&#xff0c;跟刘兄一起骑行封龙山去了&#xff0c;本来预订的正常路程从二环到封龙山大概17公里&#xff0c;来回34&#xff0c;偏偏被我这天才路痴…

2013_nanjing_online

4748 Area 4749 Parade Show 贪心模式匹配/rk-hash 4750 Count The Pairs 最小瓶颈生成树&#xff0c;统计瓶颈>c的个数。 4751 Divide Groups BFS/DFS搜索/划分成二分图 4752 Polygon 简单计算几何&#xff0c;线和多边形的交辛…

SQL Server 2005异地备份

前几天做了数据库镜像&#xff0c;现在也要来做做数据库的备份。本方案采用备份至本地然后copy到文件服务器的方法。 SQL server 2005打了sp2的补丁后好像存储过程xp_cmdshell是不能直接用的 显示高级选项&#xff08;仅需执行一次&#xff09; EXEC sp_configure show advan…

2013_changchun_online

4759 Poker Shuffle 二进制规律枚举 4760 Good Firewall 稍加变化tire树 4761 Sky 4762 Cut the Cake 概率题目&#xff0c;yy&#xff0c;公式。 4763 Theme Section 简单kmp 4764 Stone 威做福博弈&a…

从控件开发的角度看几个editor控件,Freetextbox,radtoolbar,abouteditor,cuteeditor

今天控件的开发有了很大进展&#xff0c;写些东西。在开发之前&#xff0c;我看了几个控件的源代码&#xff0c;如Freetextbox,radtoolbr,abouteditor以及cuteeditor。今天凭会议总结一下他们的特点&#xff0c;能记下来的都是他们很独特的地方。 首先是FreeTextbox 此控件是生…