ManualResetEvent详解

原文来自:http://www.cnblogs.com/tianzhiliang/archive/2011/03/04/1970726.html

1. 源码下载:

    下载地址:http://files.cnblogs.com/tianzhiliang/ManualResetEventDemo.rar

    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();
                 }
             }
             
         }
     }
}

转载于:https://www.cnblogs.com/nele/p/5552451.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值