0

Is there any way to determine if an EventWaitHandle was signaled by a call to Set(); or was timed out? an example:

private static EventWaitHandle ThreadWaiter =
        new EventWaitHandle(false, EventResetMode.AutoReset);

    private void StartLongAction()
    {
        ThreadWaiter.WaitOne(5000);

        //detect signal reason
        if(ThreadWaiter was signaled)
        {

        }
        //detect signal reason
        else if(ThreadWaiter was timedout)
        {

        }
    }

    public void HardwareEvent(EventArgs e)
    {
        ThreadWaiter.Set();
    }

The only option that I could think of is to hold a bool variable that will change on every WaitOne() and Set() call but I was wondering if the framework has a more elegant solution

Yoav
  • 3,326
  • 3
  • 32
  • 73
  • 2
    Of course, WaitOne() returns a *bool* to tell you. – Hans Passant Jan 19 '15 at 13:54
  • 1
    the returnvalue (== bool) of WaitOne specifies if the handle was signaled or not – Martin Moser Jan 19 '15 at 13:54
  • Since it looks like you are going to run this on a background thread and you are waiting on a hardware event, is there any harm in just letting it sit until you receive the event? That is, why do you need a timeout. There may be a different solution with other synchronization mechanisms if you can't just wait. – Steve Mitcham Jan 19 '15 at 13:58
  • 1
    @HansPassant & Martin thank you, I some how missed it in the [MSDN class description](http://msdn.microsoft.com/en-us/library/system.threading.eventwaithandle%28v=vs.110%29.aspx) – Yoav Jan 19 '15 at 14:04

0 Answers0