Search code examples
c#.netmultithreadingwaithandle

AutoResetEvent.WaitOne with timeout vs Thread.Sleep


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?


Solution

  • 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?