AutoResetEvent.WaitOne with timeout vs Thread.Sleep

Ryszard Dżegan picture Ryszard Dżegan · Jan 22, 2014 · Viewed 14.1k times · Source

I need a solution to perform arbitrary pause. The delay accuracy is irrelevant. What is the practical difference in such scenario between WaitHandle.WaitOne Method (TimeSpan) and Thread.Sleep Method. Are there any better solutions?

Answer

Martin James picture Martin James · Jan 22, 2014

If your spec says something like 'Always wait at least two seconds before continuing', use Sleep().

If your spec says something like 'Wait for up to two seconds for a signal from another thread and return an error if timed out' use an event object.

It's basically that simple.

There are essentially no 'performance differences' re. timing accuracy since both calls use the same mechanism for timeouts.

'Better' solutions - what is 'better'? Better in what respect?