浅谈AutoResetEvent的用法

本文介绍AutoResetEvent在C#中的应用实例,详细解释其初始化及Set与WaitOne方法的作用,并通过示例代码演示不同初始化状态下的行为差异。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;
using System.Threading;

namespace AutoResetEvent_Examples
{
    class MyMainClass
    {
        //初始的时候是没有信号的,这里的意思是指参数false
        const int numIterations = 100; //重复次数设置多少都无所谓,为让大家看清楚设置了100
        static AutoResetEvent myResetEvent = new AutoResetEvent(false);
        static int number;

        static void Main()
        {
            //创建并开始一个线程。
            Thread myReaderThread = new Thread(new ThreadStart(MyReadThreadProc));
            myReaderThread.Name = "ReaderThread";
            myReaderThread.Start();

            for (int i = 1; i <= numIterations; i++)
            {
                Console.WriteLine("Writer thread writing value: {0}", i);
                number = i;

                //发信号,说明值已经被写进去了。这里的意思是说Set是一个发信号的方法。
               myResetEvent.Set();

                //让每次循环当中有些间隔,没有其他作用,可以注释掉
              Thread.Sleep(1000);
            }

            //终止阅读线程。

            myReaderThread.Abort();
        }

        static void MyReadThreadProc()
        {
            while (true)
            {
                //在数据被作者写入之前不会被读者读取
                //在上次读取之前至少有一次。
                myResetEvent.WaitOne();
                Console.WriteLine("{0} reading value: {1}", Thread.CurrentThread.Name, number);
            }
        }
    }
}

下面我会配图进行说明,便于大家更好的理解,AutoResetEvent在使用前必须通过是例化进行初始,如:
AutoResetEvent myResetEvent = new AutoResetEvent(false);
MSDN中讲到,若要将初始状态设置为终止,则为 true;若要将初始状态设置为非终止,则为 false
如过设置true,也就是说这个Event回自动Reset,那么myResetEvent.WaitOne()就能够在程序一启动就获得运行权;就相当于一启动程序就自动运行了一次myResetEven.Set();
反之,就要等myResetEven.Set()成功运行后,myResetEven.WaitOne()才能够获得运行机会;

这里我做个实验给大家看,大家会看到,在设置为true的时候会首先看到ReaderThread reading value运行,然后才是Writer thread wrirting出现:

 如设置为false就会发现,先出现的是写的信息:


 相信大家应该明白它的初始的意义了.
上面我们提到了myResetEven.Set(),这个方法是做什么的呢,其实简单点说就相当于一个开关,如果没有执行set()方法,下面的waitOne()就等不到让它执行的通知,这样一来waitOne后面的语句也不会执行了,waitone()就会傻等下去...一直等到set()执行后才会执行它后面的操作,

我们再次做个实验,把myResetEven.Set()注释掉,结果应该是读的信息永远不会出现,如图:


 也就是说,如果 AutoResetEvent.Set()上面的线程完全被执行,AutoResetEvent.WaitOne()控制下的线程才被执行.很简单吧!!
### .NET 8.0 中 `AutoResetEvent` 的使用 在多线程编程中,同步原语对于控制多个线程之间的访问至关重要。`AutoResetEvent` 是一种用于实现线程间通信的事件对象,在一个线程完成特定操作后通知另一个等待该条件发生的线程。 #### 创建和初始化 `AutoResetEvent` 可以创建两种状态下的实例:初始状态下设置为已触发(即信号量可用),或者未触发(默认情况)。这取决于构造函数接收的一个布尔参数: ```csharp // 已触发的状态 var autoEventTrue = new AutoResetEvent(true); // 未触发的状态 var autoEventFalse = new AutoResetEvent(false); ``` #### 设置和重置事件 通过调用 `Set()` 方法来手动将事件置于已触发状态;当任意数量的等待线程被唤醒并继续执行之后,自动重置回未触发状态。如果没有任何线程正在等待,则此方法不会做任何事情[^2]。 ```csharp autoEvent.Set(); ``` 要使事件进入非激活态,可利用 `Reset()` 函数强制将其变为无信号状态,阻止其他监听者前进直到再次收到新的 Set 调用为止。 ```csharp autoEvent.Reset(); ``` #### 等待事件发生 为了阻塞当前线程直至关联的对象变成有信号或超时时间到达,应该采用如下方式之一来进行挂起处理: - **WaitOne()**: 阻塞调用线程无限期地等待,直到它接收到对应的 set 操作。 ```csharp autoEvent.WaitOne(); ``` - **WaitOne(TimeSpan)** 或 **WaitOne(Int32)** : 增加了一个最大时限参数,允许指定最长等待秒数/毫秒数作为整数值传递给这些版本的方法签名形式。 ```csharp bool result = autoEvent.WaitOne(5000); // 等待五秒钟 if (!result) Console.WriteLine("Timeout expired."); ``` #### 完整示例程序展示如何在线程之间共享资源而互不干扰 下面是一个简单的例子说明了怎样运用上述概念构建一个多生产者单消费者模式的应用场景,其中主线程负责消费数据项,而辅助线程则不断向队列里添加新元素。 ```csharp using System; using System.Threading; public class Program { private static readonly Queue<int> queue = new Queue<int>(); private static readonly AutoResetEvent producerEvent = new AutoResetEvent(false); private static readonly AutoResetEvent consumerEvent = new AutoResetEvent(false); public static void Main(string[] args) { Thread t1 = new Thread(() => Producer()); Thread t2 = new Thread(() => Consumer()); t1.Start(); t2.Start(); t1.Join(); t2.Join(); Console.ReadLine(); } private static void Producer(){ int count = 0; while (count++ < 10){ lock(queue){ queue.Enqueue(count); Console.WriteLine($"Produced item {count}"); } producerEvent.Set(); // Notify the consumer thread that an item is available. consumerEvent.WaitOne(); // Wait until notified by the consumer to continue producing items. } } private static void Consumer(){ do{ producerEvent.WaitOne(); // Wait for notification from the producer. lock(queue){ if(queue.Count > 0){ int consumedItem = queue.Dequeue(); Console.WriteLine($"Consumed item {consumedItem}"); if(consumedItem >= 10){ break;} // Exit after consuming all produced items. } } consumerEvent.Set(); // Signal back to allow further production of items. }while(true); } } ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值