栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

c#基础知识之AutoResetEvent

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

c#基础知识之AutoResetEvent

摘要

AutoResetEvent:msdn的描述是通知正在等待的线程已发生事件。此类不能被继承。也就是说它有那么一个时间点,会通知正在等待的线程可以做其它的事情了。

AutoResetEvent 

该类有一个带bool类型参数的构造函数


#region Assembly mscorlib.dll, v4.0.0.0// C:Program Files (x86)Reference AssembliesMicrosoftframework.NETframeworkv4.5mscorlib.dll#endregionusing System;using System.Runtime.InteropServices;namespace System.Threading
{    // Summary:    //     Notifies a waiting thread that an event has occurred. This class cannot be    //     inherited.
   [ComVisible(true)]    public sealed class AutoResetEvent : EventWaitHandle
   {        // Summary:        //     Initializes a new instance of the System.Threading.AutoResetEvent class with        //     a Boolean value indicating whether to set the initial state to signaled.        //
       // Parameters:        //   initialState:        //     true to set the initial state to signaled; false to set the initial state        //     to non-signaled.
       public AutoResetEvent(bool initialState);
   }
}


该bool值指示初始化的时候是否设置为终止状态。

AutoResetEvent allows threads to communicate with each other by signaling.">AutoResetEvent allows threads to communicate with each other by signaling.Typically, you use this class when threads need exclusive access to a resource.

AutoResetEvent allows threads to communicate with each other by signaling.">AutoResetEvent允许线程之间通过信号进行通信。通常,当线程独占访问资源的时候你可以使用该类。

AutoResetEvent allows threads to communicate with each other by signaling.">注意

AutoResetEvent allows threads to communicate with each other by signaling.">This type implements the IDisposable interface.When you have finished using the type, you should dispose of it either directly or indirectly.To dispose of the type directly, call its Dispose method in a try/catch block.

AutoResetEvent allows threads to communicate with each other by signaling.">该类实现了IDisposable接口。当你使用结束的时候,你需要直接或者间接的释放它。直接释放可以通过在try/catch块中调用它的Dispose方法。

AutoResetEvent allows threads to communicate with each other by signaling.">一个线程等待在AutoResetEvent上调用WaitOne信号。如果AutoResetEvent为未触发状态,则线程会被阻止,并等待当前控制资源的线程通过调用Set来通知资源可用。

AutoResetEvent allows threads to communicate with each other by signaling.">调用Set信号,AutoResetEvent将释放一个等待中的线程。当AutoResetEvent被设置为已触发状态时,它将一直保持已触发状态直到一个等待的线程被激活,然后它将自动编程未触发状态。如果没有任何线程在等待,则状态将无限期地保持为已触发状态。

AutoResetEvent allows threads to communicate with each other by signaling.">如果当AutoResetEvent为已触发状态时调用WaitOne,则线程不会被阻止。AutoResetEvent将立即释放线程并返回到未触发状态。

AutoResetEvent allows threads to communicate with each other by signaling.">There is no guarantee that every call to the Set method will release a thread.If two calls are too close together, so that the second call occurs before a thread has been released, only one thread is released.It is as if the second call did not happen.Also, if Set is called when there are no threads waiting and the AutoResetEvent is already signaled, the call has no effect.

AutoResetEvent allows threads to communicate with each other by signaling.">也不能保证每次调用set方法就释放一个线程。如果两次调用太近,以至于第二次调用在释放线程时,只有一个线程被释放。就好像第二次调用没发生一样。另外,如果设置为当没有线程等待和AutoResetEvent已经发出信号,则调用没有影响。

AutoResetEvent allows threads to communicate with each other by signaling.">你可以通过构造函数的bool值参数控制初始化时AutoRestEvent的状态。若要将初始状态设置为终止,则为 true;若要将初始状态设置为非终止,则为 false。

AutoResetEvent allows threads to communicate with each other by signaling.">示例

下面的示例将演示如何使用AutoResetEvent通过Set方法在用户按下回车键的时候释放一个线程。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Wolfy.AutoResetEventDemo
{

   class Program
   {
       const int numIterations = 100;
       //初始化为非终止状态。
       static AutoResetEvent myResetEvent = new AutoResetEvent(false);
       static int number;

       static void Main(string[] args)
       {
           //创建并启动读线程
           Thread myReaderThread = new Thread(new ThreadStart(MyReaderThreadProc));
           myReaderThread.Name = "ReaderThread";
           myReaderThread.Start();
           for (int i = 0; i < numIterations; i++)
           {
               Console.WriteLine("Writer thread writing value:{0}", i);
               number = i;
               //给读线程信号
               myResetEvent.Set();
               Thread.Sleep(1);
           }
           myReaderThread.Abort();
       }

       private static void MyReaderThreadProc()
       {
           while (true)
           {
               myResetEvent.WaitOne();
               Console.WriteLine("{0} reading value:{1}", Thread.CurrentThread.Name, number);
           }
       }
   }
}

执行顺序

AutoResetEvent allows threads to communicate with each other by signaling.">运行结果

AutoResetEvent allows threads to communicate with each other by signaling.">

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/231549.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号