-2

Is there a way to make an application, or a thread, run at a fixed rate?

I'm trying to do some deterministic simulations between networked clients and would like both machines (Windows) to run or process the data at a fixed, unchanging rate. Is this possible?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Lanefox
  • 11
  • 1
  • 1
  • 6
  • You'll have to be more specific about what you mean. Time has many quirks in computer science that this question does not appear to fully appreciate. Please describe what you are trying to do in more specific detail. – J... Apr 07 '21 at 21:37

1 Answers1

1

You can't make an existing application to run at particular speed (there could be VM based solutions that normalize executions speed, but I'm not aware of those myself).

If you writing your own code usual approach is to basically sleep between processing the next iteration. It is commonly done for (simple) games where is less processing than CPU power.

Pseudocode:

 while(true)
 {
     executeStep();
     await Task.Delay(GetTimeforNextStep() - DateTime.Now.Utc);
 }

Note that precise synchronization is not possible with consumer grade OS (Windows/Linux/MacOS) - you need RTOS for a precise millisecond level timing.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • You're assuming they want a loop to poll at a fixed rate. It's not at all clear from their question that this is what they are asking. – J... Apr 07 '21 at 21:41
  • recomputing the `Task.Delay` argument is a rather poor substitute for a Win32 "Waitable Timer" object, but [there isn't a convenient way to use the waitable timer from C#](https://stackoverflow.com/q/15858751/103167). – Ben Voigt Apr 07 '21 at 21:41
  • This is pretty much the answer. I want to write a deterministic simulation in lockstep over the network but avoid forcing clients to wait on each other to step (instead, utilize rollback to reconcile clients getting data out of step). Thus, I wanted to know if I could start all clients and simply run them at the same rate. I understand this can't be guaranteed, although I am curious (and will probably explore) how close I can get on midrange windows pc hardware using tasks with delays. – Lanefox Apr 07 '21 at 21:46
  • I've found that this approach would have poor timing resolution and is unlikely to yield consistent results. More info here: https://stackoverflow.com/questions/3744032/why-are-net-timers-limited-to-15-ms-resolution – Lanefox Apr 08 '21 at 14:31
  • @Lanefox Entirely unsurprising. If you had explained what you were trying to do you would get better advice. For an example of time synchronization between networked systems, perhaps you could study the problem of [streaming music](https://stackoverflow.com/q/2795031/327083) in sync over a network. It may not apply to your problem, but again, you haven't told us what you're trying to do, so trying to help we are reduced to guessing. It goes without saying that this is not conducive to finding the best solution. – J... Apr 09 '21 at 10:03