Forum C# AddTimer interval is not exact, am I doing something wrong?

AddTimer interval is not exact, am I doing something wrong?

Postby qugengames » October 5th, 2018, 3:08 pm

I set a simple method called "GameUpdate", using AddTimer(GameUpdate, 25); during the GameStarted event.
I have put a System.Diagnostics.Stopwatch to measure how often the method is called, but to my surprise this is how it looks every call:
45, 22, 36, 26, 36, 27, 33, 26, 30

Which means the first call took 45 milliseconds since the AddTimer to the method actually being triggered.
Second time was 22 milliseconds, and so on...

Why is this not consistent as the 25 Milliseconds which was specified using the AddTimer function?
qugengames
 
Posts: 36
Joined: April 24th, 2018, 11:19 pm

Re: AddTimer interval is not exact, am I doing something wro

Postby qugengames » October 5th, 2018, 3:14 pm

For those interested, what I ended up doing is use the Stopwatch to measure the time elapsed in seconds since the AddTimer until the start of the GameUpdate method, and then take the time at the end of the GameUpdate method until the start of such method again, and consider it being the DeltaTime which I pass to my game methods, this makes everything consistent instead of using a fake fixed elapsed deltaTime which is set during the AddTimer method.
qugengames
 
Posts: 36
Joined: April 24th, 2018, 11:19 pm

Re: AddTimer interval is not exact, am I doing something wro

Postby Henrik » October 16th, 2018, 4:10 am

The underlying cause is that .Net simply doesn't make real-time guarantees, only best-effort guarantees, so this is as good as it gets for now.

You also have to remember that all your clients are going to be connected to the server over the internet, which makes absolutely zero timing guarantees, so you can't really rely on precise server timing, because some clients will be maybe hundreds of milliseconds behind everyone else.
Henrik
.IO
 
Posts: 1880
Joined: January 4th, 2010, 1:53 pm

Re: AddTimer interval is not exact, am I doing something wro

Postby qugengames » October 24th, 2018, 10:54 pm

Henrik wrote:The underlying cause is that .Net simply doesn't make real-time guarantees, only best-effort guarantees, so this is as good as it gets for now.

You also have to remember that all your clients are going to be connected to the server over the internet, which makes absolutely zero timing guarantees, so you can't really rely on precise server timing, because some clients will be maybe hundreds of milliseconds behind everyone else.


Hi Henrik,
I guess the most important question here is why would someone look to rely on exact timer ticks,
In my case I was using a fixed value for time steps on a physics library, since AddTimer was running at inconsistent times I ended up using a Delta Time using a stopwatch to measure elapsed milliseconds,

Regards
qugengames
 
Posts: 36
Joined: April 24th, 2018, 11:19 pm


Return to C#