Background
I am trying to write an application that does the following:
SomeBlockingMethod
.SomeUnblockingMethod
from another thread.SomeUnblockingMethod
is called, the routine inside of SomeBlockingMethod
will continue.Note, the first thing I do will be to call the SomeBlockingMethod
, and then later on I will call the SomeUnblockingMethod
. I am thinking about using a Monitor.Wait/Monitor.Pulse
mechanism to achieve this. The only thing is, when one calls Monitor.Wait
, you cannot block initally unless the object
involved has already been locked by something else (or at least not that I know of)... But, I want blocking to be the first thing I do... So this leads me into my question...
Question
Is there some way I can implement Monitor.Wait
to initially block until a call to Monitor.Pulse
is made?
You can use AutoResetEvent instead.
AutoResetEvent ar = new AutoResetEvent(false); // false set initial state as not signaled
Then you can use ar.WaitOne()
to wait and ar.Set()
to signal waiting processes.
You should use Monitor when you want to protect a resource or you have a critical section. If you want to have a signaling mechanism then AutoResetEvent
or ManualResetEvent
sounds like a better option.