-2

Why wouldn't the Vote(); function run wihtin this timer?

The message box one works but vote one doesn't

public void Timer()
{
        var timespan = new TimeSpan(0, 0, 20);
        var timer = new System.Timers.Timer(timespan.TotalMilliseconds);
        timer.Elapsed += (o, e) =>
        {
            MessageBox.Show("Worked");
            Vote();
        };
        timer.Start();
}

Vote is above this function within this code, Thanks, Oliver.

2 Answers2

1

The Timer.Elapsed event suppresses all exceptions, causing you to not see any errors. Add your own error handling in the Elapsed event to see what's failing.

From MSDN:

The Timer component catches and suppresses all exceptions thrown by event handlers for the Elapsed event. This behavior is subject to change in future releases of the .NET Framework.

Hand-E-Food
  • 12,368
  • 8
  • 45
  • 80
  • Uhm, no. Using a slash as a path delimiter in Windows is perfectly OK. – Christian.K Feb 27 '12 at 06:26
  • @Christian.K, I never knew that. Thanks! Any idea when that was introduced? Edited my answer to keep the facts straight. – Hand-E-Food Feb 27 '12 at 22:00
  • I don't have any reference, but I knew about it since "Windows NT", I guess. Anyway, [this SO question](http://stackoverflow.com/q/7314606/21567) has more information about directory separators on Windows (hint: Korean version, etc. ;-) – Christian.K Feb 28 '12 at 05:16
0

The elapsed timer code can run on a separate thread. If it does you could have a cross threaded violation. Can't know for sure without seeing the code for Vote. If you want to make sure the lambda is running writing a message out the console would be less disruptive than using a winform dialog box.

Tod
  • 8,192
  • 5
  • 52
  • 93