AutoResetEvent 重置方法

发布于 2024-11-04 15:41:16 字数 91 浏览 5 评论 0原文

有人可以介绍 AutoResetEvent.Reset() 方法的用例吗? 我何时以及为什么要使用此方法? 我了解 WaitOne 和 Set,但这对我来说相当不清楚。

Could someone introduce an use case for AutoResetEvent.Reset() method ?
When and why I would like to use this method ?
I understand WaitOne and Set but this is quite unclear for me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

婴鹅 2024-11-11 15:41:16

是的,只要等待事件的线程收到信号,AutoResetEvent 就会自动重置其状态。但是,给定事件可能不再有效,并且自最初设置后没有线程等待 AutoResetEvent。在这种情况下,Reset 方法就变得有用了

Yes the AutoResetEvent will automatically reset it's state whenever a thread which is waiting on the event is signaled. However it's possible that a given event is no longer valid and no thread has waited on an AutoResetEvent since it was originally set. In that scenario the Reset method becomes useful

幸福不弃 2024-11-11 15:41:16

看起来它只是继承自 EventWaitHandle。也许对于同样继承自该类的 ManualResetEvent 更有用?

Looks like it's just inherited from EventWaitHandle. Probably more useful with the ManualResetEvent which also inherits from that class?

緦唸λ蓇 2024-11-11 15:41:16

该方法继承自基类 EventWaitHandle,用于(重新)将 AutoResetEvent 设置为其“阻止”状态。

因为 AutoResetEvent 一旦收到信号就会自动进入该状态,因此您通常不会在代码中看到此方法,但对于从 EventWaitHandle 派生的其他类来说,这会很重要更有用!

The method is inherited from the base class EventWaitHandle and is used to (re)set an AutoResetEvent to its "blocked" state.

Because AutoResetEvent will automatically enter that state as soon as it's signalled, you'll typically never see this method used in code, but for other classes deriving from EventWaitHandle it would be much more useful!

情丝乱 2024-11-11 15:41:16

如果 AutoResetEvent 生产者想要清除事件,您可以使用 Reset()。这样,您就可以安全地“重置”事件,而不必知道当前是否已发出信号。如果生产者使用 WaitOne 来“重置”它自己的事件,则存在死锁的风险(即永远不会返回,因为事件没有发出信号且生产者线程被阻塞)。

If the AutoResetEvent producer wants to clear the event, you would use Reset(). This way, you can safely "reset" the event without having to know if it's currently signaled. If the producer used WaitOne to "reset" it's own event, there is a risk that you could deadlock (i.e. never return since the event isn't signaled and the producer thread is blocked).

老娘不死你永远是小三 2024-11-11 15:41:16

重置

将事件的状态设置为无信号
,请参阅 EventWaitHandle 类

示例,

using System;
using System.Threading;
namespace ThreadingInCSharp.Signaling
{
    class Program
    {
        static EventWaitHandle _waitHandle = new AutoResetEvent(false);
        static void Main(string[] args)
        {
            //The event's state is Signal
            _waitHandle.Set();
            new Thread(Waiter).Start();
            Thread.Sleep(2000);
            _waitHandle.Set();
            Console.ReadKey();
        }
        private static void Waiter()
        {
            Console.WriteLine("I'm Waiting...");
            _waitHandle.WaitOne();
            //The word pass will print immediately 
            Console.WriteLine("pass");
        }
    }
}

使用 重置,

using System;
using System.Threading;
namespace ThreadingInCSharp.Signaling
{
    class Program
    {
        static EventWaitHandle _waitHandle = new AutoResetEvent(false);
        static void Main(string[] args)
        {
            //The event's state is Signal
            _waitHandle.Set();
            _waitHandle.Reset();
            new Thread(Waiter).Start();
            Thread.Sleep(2000);
            _waitHandle.Set();
            Console.ReadKey();
        }

        private static void Waiter()
        {
            Console.WriteLine("I'm Waiting...");
            _waitHandle.WaitOne();
            //The word will wait 2 seconds for printing
            Console.WriteLine("pass");
        }
    }
}

Reset:

Sets the state of the event to nonsignaled
,See EventWaitHandle Class

Sample,

using System;
using System.Threading;
namespace ThreadingInCSharp.Signaling
{
    class Program
    {
        static EventWaitHandle _waitHandle = new AutoResetEvent(false);
        static void Main(string[] args)
        {
            //The event's state is Signal
            _waitHandle.Set();
            new Thread(Waiter).Start();
            Thread.Sleep(2000);
            _waitHandle.Set();
            Console.ReadKey();
        }
        private static void Waiter()
        {
            Console.WriteLine("I'm Waiting...");
            _waitHandle.WaitOne();
            //The word pass will print immediately 
            Console.WriteLine("pass");
        }
    }
}

Using Reset,

using System;
using System.Threading;
namespace ThreadingInCSharp.Signaling
{
    class Program
    {
        static EventWaitHandle _waitHandle = new AutoResetEvent(false);
        static void Main(string[] args)
        {
            //The event's state is Signal
            _waitHandle.Set();
            _waitHandle.Reset();
            new Thread(Waiter).Start();
            Thread.Sleep(2000);
            _waitHandle.Set();
            Console.ReadKey();
        }

        private static void Waiter()
        {
            Console.WriteLine("I'm Waiting...");
            _waitHandle.WaitOne();
            //The word will wait 2 seconds for printing
            Console.WriteLine("pass");
        }
    }
}
╰沐子 2024-11-11 15:41:16

使用 Reset() 时应使用 ManualResetEvent,因为当线程发出信号时 AutoResetEvent 会自行重置。

You should use ManualResetEvent when using Reset(), as AutoResetEvent resets itself when a thread has become signaled.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文