Reputation: 19885
I get confused as in when to use them.. I know all of these methods make a thread block.
So what are their particular case usage.
Also what is preferred technique of Wait().. (as in ManualResetEvent ONLY?)
Upvotes: 1
Views: 1009
Reputation: 23266
Sleep : I don't recommend the use of this unless it's a background thread that does constant polling as it can mask race conditions. Yields timeslice for a MINIMUM of the parameter sent to it, timeslicing quantums in the OS determine the actual granularity (which could be more time than you tell it)
Wait : In .NET this is a spinwait, used for locking. It puts the processor into a small loop (usually 2 instructions), basically a sleep that blocks the thread but continues executing insteading of yielding its timeslice. Alternatively could be WaitOne which waits on a WaitHandle to receive a signal. In this case the thread waits up to the amount of time specified for the signal to be received and then unblocks or you can wait forever until the signal is received (can be used to implement timeouts on async operations, also has other uses)
Interrupt : Interrupt a thread that is either in the wait, sleep or join blocking state.
Join: join the thread to the current context thread and unblocks once the joined thread finishes, used to wait for something to finish that is needed to continue
Upvotes: 3
Reputation: 12119
There is an excelent free eBook by Joseph Albahari, author of LINQPad and several C# bestsellers, on Threading in C#. If you put some effort in going through that material your confusion should vanish...
Upvotes: 2