Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 940
Ryszard Dżegan Avatar asked Jan 22 '14 11:01

Ryszard Dżegan


2 Answers

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?

like image 60
Martin James Avatar answered Sep 28 '22 09:09

Martin James


1.Thread.Sleep(timeout) causes an unconditional wait before execution is resumed.

2.WaitOne(timeout) causes the thread to wait until either

  • the event is triggered,
  • The timeout is reached
like image 34
santosh singh Avatar answered Sep 28 '22 09:09

santosh singh