views:

41

answers:

4

Hi, I'm using the ManualResetEvent WaitOne(timeout) method and set the timeout value to 30ms.

I log using log4net at either side of the WaitOne. The log messages show the WaitOne returned false after only waiting for 22ms. What would cause this? A .Net bug?

Thanks in advance.

A: 

In general, don't depend on timeouts to be exact. They'll be approximately correct, not not necessarily perfect. For the same reason, you shouldn't be depending on calls like Thread.Sleep() for timing, the OS doesn't ensure that these calls are timed exactly

LorenVS
+1  A: 

Either the event is signaled early, or you have timer inaccuracy playing a role in this, since the system clock is, by default, accurate to only 15.625 ms.

Michael Goldshteyn
+3  A: 

From the Win32 documentation on Wait Functions which the .NET methods ultimately use (http://msdn.microsoft.com/en-us/library/ms687069.aspx):

Wait Functions and Time-out Intervals

The accuracy of the specified time-out interval depends on the resolution of the system clock. The system clock "ticks" at a constant rate. If the time-out interval is less than the resolution of the system clock, the wait may time out in less than the specified length of time. If the time-out interval is greater than one tick but less than two, the wait can be anywhere between one and two ticks, and so on.

Michael Burr
+1 Was looking for the MSDN article on this, you beat me to it
LorenVS
A: 

Internally, the WaitOne method from .NET calls the Windows WaitOne method. This is not a bug but normal Windows behavior.

Pieter