For this purpose, not only sleeping is not a "better way"; it is totally unacceptable because it causes
race condition:
http://en.wikipedia.org/wiki/Race_condition[
^].
Event if it works for you, it's just a matter of chance. It is not a legitimate way of thread synchronization. Sleeping has its uses, but this is beyond this question. Forget it for now.
If you really need one thread to wait for another one, you can use the class
System.Threading.ManualResetEvent
, which is merely a kind of the other class with specialized constructor,
System.Threading.EventWaitHandle
, which you also can use with
System.Threading.EventResetMode.ManualReset
:
http://msdn.microsoft.com/en-us/library/system.threading.manualresetevent%28v=vs.110%29.aspx[
^],
http://msdn.microsoft.com/en-us/library/system.threading.eventwaithandle(v=vs.110).aspx[
^].
I hope you can figure out how to use it from this MSDN documentation. When one thread calls
EventWaitHandle.WaitOne
, it is put to a special
wait state which wastes no CPU time: the calling thread is switched off and never scheduled back to execution until it is waken up, which can be done by such conditions as abort, timeout, and, importantly, the call from other thread, which signals the same instance of
EventWaitHandle
with the call to
EventWaitHandle.Set
.
—SA