1

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).

  • 2
    Why are you using DoEvents()....? not a recommended thing to do if you are doing multi-Threading – MethodMan Dec 18 '12 at 13:07
  • I don't read German, sorry... – JYL Dec 18 '12 at 13:11
  • 1
    here is a Stackoverflow post that you can read and there is good explanation on why you should not use Application.DoEvents() http://stackoverflow.com/questions/5181777/use-of-application-doevents – MethodMan Dec 18 '12 at 13:12
  • DoEvents() doesn't really bother me here as the form is hidden. But that part of the code MUST run in the GUI thread while simultaneously capturing WM_USER messages, so there seems to be no other choice here. Removing the DoEvents() doesn't solve the problem either, DateTime still crashes. –  Dec 18 '12 at 13:16
  • The translationof your exception read: `The value to add was out of range.` What is the value of the ms var passed in. – Steve Dec 18 '12 at 13:20
  • 1
    @Steve: The value passed is always 5 at the moment. Simply calling DateTime.Now throws a similar exception, with the Add() call buried inside mscorlib. Only DateTime.UtcNow doesn't throw... –  Dec 18 '12 at 13:24
  • Strange, just tested with LinqPad and works without problem here. Same Win7 64bit and Net 4.0.3. Restart? – Steve Dec 18 '12 at 13:28
  • @Steve: See edit above: In VmWare it works, on the PCs it doesn't. Looks like a configuration problem to me... –  Dec 18 '12 at 13:33
  • 4
    Are your computers running on the Mayan calendar? – Rawling Dec 18 '12 at 13:33
  • That exception can't happen with a value of 5. Is it possible this value is read from a configuration file, and on the computers you've tried that file has changed or become invalid? – Jon B Dec 18 '12 at 13:44
  • No config files, nothing. `while (true) SleepWithEventLoop(5);` fails. –  Dec 18 '12 at 14:02
  • Stupid question, but given the weirdness, I figure it's worth asking: What happens if you change your method to take a double and call it with 5.0 (instead of counting on the implicit cast from int to double)? – Joel Rondeau Dec 18 '12 at 14:46

1 Answers1

0

When I have code that works properly on one computer and incorrectly on a second one, I expect the problem is the DLLs being used by my application. So my recommendation is this:

  • Run the app on both your machine and the VM where it works.
  • Run ProcessExplorer on both machines.
  • Show the DLLs in the lower pane of ProcessExplorer.
  • Compare the DLL versions to see which, if any are different.
Joel Rondeau
  • 7,486
  • 2
  • 42
  • 54
  • Good idea, but it's the same DLLs. Looks more like a misconfiguration or some interference caused by those remote management / virus scanners / other tools to me, which are not present on the VMs... –  Dec 19 '12 at 09:52