1

This is my first post on SO. Usually just lurking, which helps me a lot. But I can't solve this one and need your help! I have made a test program using Windows Forms. It contains two buttons (a start and a stop button). When the start button is clicked, for every ms (millisecond), a log-function is being called, which extracts some basic information (or so I thought). The test stops after 2 minutes, where the stop function is clicked. When the test has been running for 2 minutes, there are only 7789 measurements. The test doesn't add up - any ideas, why is this so? I've attached a screenshot of the code of the start and stop buttons. enter image description here

//Start-button

private void button1_CLick(object sender, EventArgs e)
{
     TestInitiated = true;
     LogTimer = new System.Windows.Forms.Timer();
     LogTimer.Tick += new EventHandler(stopwatch_tick);
     LogTimer.Enabled = true;
     LogTimer.Interval = 1;
     LogTimer.Start();
}

//STop-Button
private void button2_Click)object sender, EventArgs e)
{
     TestInitiated = false;
     LogTimer.Stop();
     LogTimer.Enabled = false;
}
FlyingFoX
  • 3,379
  • 3
  • 32
  • 49
tigram10
  • 19
  • 2
  • The [System.Windows.Forms.Timer](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.timer) doesn't have that resolution (see the Remarks section). Also, see [High resolution timer](https://stackoverflow.com/a/45097518/7444103) – Jimi Dec 16 '19 at 17:07

1 Answers1

1

Windows Forms timers are accurate to 55 milliseconds, you need to use a System.Timers.Timer if you need more accuracy.

https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.timer

ZafarB
  • 36
  • 4
  • Thank you for the answer! Is it possible to use System.Timers.Timer while keeping the GUI in WinForm? – tigram10 Dec 16 '19 at 17:13
  • 1
    @tigram10 See the [System.Timers.Timer.SynchronizingObject](https://learn.microsoft.com/en-us/dotnet/api/system.timers.timer.synchronizingobject) property. Note that if you have some processing between Elapsed events that take more than the Interval time, the events will overlap and you may have race conditions (and/or re-entrancy) issues. – Jimi Dec 16 '19 at 17:21