• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

[c#基础]AutoResetEvent

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

摘要

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

AutoResetEvent 

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

#region Assembly mscorlib.dll, v4.0.0.0
// C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\mscorlib.dll
#endregion

using 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值指示初始化的时候是否设置为终止状态。

Typically, you use this class when threads need exclusive access to a resource.

AutoResetEvent允许线程之间通过信号进行通信。通常,当线程独占访问资源的时候你可以使用该类。

注意

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.

该类实现了IDisposable接口。当你使用结束的时候,你需要直接或者间接的释放它。直接释放可以通过在try/catch块中调用它的Dispose方法。

一个线程等待在AutoResetEvent上调用WaitOne信号。如果AutoResetEvent为未触发状态,则线程会被阻止,并等待当前控制资源的线程通过调用Set来通知资源可用。

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

如果当AutoResetEvent为已触发状态时调用WaitOne,则线程不会被阻止。AutoResetEvent将立即释放线程并返回到未触发状态。

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.

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

你可以通过构造函数的bool值参数控制初始化时AutoRestEvent的状态。若要将初始状态设置为终止,则为 true;若要将初始状态设置为非终止,则为 false。

示例

下面的示例将演示如何使用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);
            }
        }
    }
}

执行顺序

运行结果

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#存取数据为所欲为(一)发布时间:2022-07-13
下一篇:
C#局域网桌面共享软件制作(一)发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap