Using C#, .NET 4.0 update 4.0.3, Windows 7 64bit:
For reasons beyond my responsibilities, I have to let the GUI thread "sleep" while handling messages in the background. Up until now, this worked flawlessly:
private void SleepWithEventLoop(int ms)
{
var timeout = DateTime.UtcNow.AddMilliseconds(ms);
while (DateTime.UtcNow < timeout && !disposed)
{
System.Windows.Forms.Application.DoEvents();
Thread.Sleep(1);
}
}
Since I booted the PC this morning, for no apparent reason, I start getting exceptions like this (snippet from log file):
2012-12-18 12:50:41.204 | - None - | Exception | Exception caught: Der hinzuzufügende Wert war außerhalb des Bereichs.
| | | Parametername: value
| | | Details:
| | | bei System.DateTime.Add(Double value, Int32 scale)
| | | bei System.DateTime.AddMilliseconds(Double value)
| | | bei com.<...>.CanOpenProxyServer.SleepWithEventLoop(Int32 ms) in C:\Users\<...>\CanOpenProxy.cs:Zeile 78.
| | | bei com.<...>.CanOpenProxyServer.HandleConnection(MessageChannel channel) in C:\Users\<...>\CanOpenProxy.cs:Zeile 55.
I've had some problems with DateTime.Now concerning DST that went away after switching to DateTime.UtcNow, but the exception above remains. IT dept didn't install any updates lately, and I certainly didn't install or remove any programs or mess with the registry. How can DateTime just suddenly "break"?!?
I copied the code above into a new project to test it alone with
while (true) SleepWithEventLoop(5);
and it still crashed. Re-installing .NET didn't work either. Any ideas?
EDIT: The same problem on two colleagues' PCs, but NOT on a freshly installed VmWare system. Starts to look like our company PCs are configured a little funnily. Does DateTime need any "special" configuration?
EDIT 2: So I "fixed" the problem by P-Invoking into a small C DLL that exposes the time.h functions, wrapped in a DateTime like class. Search/replace in all projects. I feel dirty doing this, but it solves at least the symptoms. I'll look into this again when the schedule gets a little less stressful (yeah, right).