1

I was dealing with code that has some calls to ManualResetEvent's WaitOne call.

From the MSDN documentation,

If timeout is zero, the method does not block. It tests the state of the wait handle and returns immediately.

Now, this is my piece of code:

   Console.WriteLine("abc");
   if (manualResetEventObject.WaitOne(0, false))
       return;

   Console.WriteLine("def");
   //More function calls

Here, based on the doc, I'd expect it to print abc, return from WaitOne and print def. manualResetEventObject is not signaled at this point, and so WaitOne's return value should be false.

However, def is never printed and I see a ThreadInterruptedException being thrown (there's a try/catch block enclosing this).

I don't understand why this doesn't print def or why the exception is thrown. What exactly is going on here ?

Cygnus
  • 3,222
  • 9
  • 35
  • 65
  • Looks like another thread has [interrupted](http://msdn.microsoft.com/en-us/library/system.threading.thread.interrupt%28v=vs.110%29.aspx) your thread before the call to `WaitOne()`. – alsed42 Feb 26 '14 at 13:28
  • I am curious why you need this overloaded waitone vs the general waitone (timespan) or waitone with int. Are you switching contexts? Also the ThreadInteruptedException is not part of the exceptions thrown by the waitone method you are using. – TYY Feb 26 '14 at 13:55

0 Answers0