WaitHandle.WaitOne returns true in IDE debug but not in Immediate Window

39 views Asked by At

I can do this in my code I'm stepping though..

01 // In another sub
02 EventWaitHandle waitHandle = new EventWaitHandle(false, EventResetMode.AutoReset, "MyTestHandleName");
03
04 // In the current code block I'm debugging
05 bool retVal = false;
06 retVal = waitHandle.WaitOne(1000);
07 if (retVal) { }

Then I put brakes on line 06 and 07. At line 06 retVal is the default value of FALSE. In the Immediate Window I run waitHandle.WaitOne(1000) and it returns FALSE.

Then I press F10 and it finishes running line 06, then I check the value of retVal and see it's TRUE. I then run waitHandle.WaitOne(1000) in the Immediate Window again and it still returns FALSE.

Question: Why is the Immediate Window returning a different value then the actual IDE during debug?

1

There are 1 answers

4
shingo On

The immediate window is not a magical tool that can freeze time. It only executes the statements you enter, so what you do is equivalent to calling WaitOne twice. If the first call is successful, as this WaitHandle is AutoReset, the second call will naturally fail unless you release it.

Result false

Result true