Search code examples
c#timertiming

How can I trigger a method at precise intervals? System.Timers.Timer seems to loose sync


In my current project I want to send predefined Haptic Information synced to a Video. For Example the Video has strong Wind and I want to send a sensation. This haptic data needs to be calculated.

I want to to calculate and send data at a fix interval of 0.1 seconds. The plan is to send the calculated data and then start the calculation for the next data. For that i used a System.Timers.Timer with AutoReset = true and the Elapsed Event.

The condition i try to achieve are like this.

  • The data does not have to be precicely sent every 0.1 seconds.
  • The calculated data is supposed to be for a moment within a video, so the internal Timer and the video timer need to be roughly in sync.
  • If calculating the data takes longer than the 0.1 seconds we simply resend the last calculated package again.

However i stumbled upon a problem with the sync of the Timer. Here is a log every time I send a Sensation with the tick count on the left and the actual passed time (Determined by System.Diagnostics.Stopwatch) with the tick number on the left and the milliseconds since start of execution on the right.

1 / 106
2 / 216
3 / 326
4 / 448
5 / 554
6 / 660
7 / 768
8 / 875
9 / 983
10 / 1090
11 / 1213
12 / 1322
13 / 1431
14 / 1539
15 / 1647
16 / 1754
17 / 1861
18 / 1968
19 / 2075
20 / 2197
...
154 / 16885
155 / 16994
156 / 17100
157 / 17207
158 / 17317
159 / 17426

Basicly every 0.1 sec tick which sends the data is executed slightly later than 0.1 seconds (which is ok) and the next send delay stacks on top of that delay (which is not ok), so after 15 Seconds of execution the timer was already about 2 Seconds behind.

It looks like the new interval is only started after executing the Elapsed Events, rather than immediately. The code I had looked something like this.

private int tick = 0;

private System.Timers.Timer timer;
private System.Diagnostics.Stopwatch watch;

private Constructor() {
    timer = new System.Timers.Timer(100);
    timer.Elapsed += streamSensation; // simply sends the Sensation (and created above logs)
    timer.Elapsed += calcManagerTick; // mainly calculates the Sensation for next Tick
    timer.AutoReset = true;
    timer.Enabled = false;
}

private void streamSensation(Object source, ElapsedEventArgs e) {
    // Send data
    tick++;
    Console.WriteLine(tick + " / " + watch.ElapsedMilliseconds);
}

private void calcManagerTick(Object source, ElapsedEventArgs e) {
    // load and do stuff based on tick variable
}

private void start() {
    if (!timer.Enabled) {
        watch = Stopwatch.StartNew();
        timer.Start();
    }
}

The only solutions that comes to my mind are kind of hacky, like recalculating the interval every cycle by using the Stopwatch, which I don't want to do, because I am sure there are better solutions.


Solution

  • @NPras helped out by mentioning that the .Net version may be outdated. I was indeed running on a old .Net 4 Version. Or atleast a config file said something like

    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
    

    Maybe its only the .Net core Version. I dont know. A new project solved the issue. If i had to take a different quess id say I once picked "Windows Forms App" and the other time "Windows Forms App (.Net Framework)"... whatever the difference is.