AutoResetEvent Reset method
Asked Answered
S

6

21

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.

Sigismond answered 3/5, 2011 at 14:27 Comment(1)
Why a vote to close? This question seems perfectly clear and useful to me...Acescent
D
10

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

Denunciation answered 3/5, 2011 at 14:38 Comment(2)
But what is a benefit on calling Reset() in such case ?Sigismond
@Sigismond it prevents any thread which hasn't yet called wait, but will at some point in the future, from being activated for an event which is no longer valid. Note: This in itself is not enough to prevent a race condition but it can be a part of a larger solutionDenunciation
H
1

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

Hyposensitize answered 3/5, 2011 at 14:31 Comment(0)
A
1

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!

Acescent answered 3/5, 2011 at 14:33 Comment(0)
O
1

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).

Obsequies answered 3/5, 2011 at 14:35 Comment(0)
C
1

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");
        }
    }
}
Catchup answered 16/6, 2017 at 3:35 Comment(0)
P
0

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

Political answered 3/5, 2011 at 14:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.