开发者

ManualResetEvent WaitOne(timeout) returns early. Any ideas why?

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 message开发者_如何学编程s show the WaitOne returned false after only waiting for 22ms. What would cause this? A .Net bug?

Thanks in advance.


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.


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.


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜