- Stopwatch , . . :
int noofrunspersecond = 30;
long ticks1 = 0;
long ticks2 = 0;
double interval = (double)Stopwatch.Frequency / noofrunspersecond;
while (true) {
ticks2 = Stopwatch.GetTimestamp();
if (ticks2 >= ticks1 + interval) {
ticks1 = Stopwatch.GetTimestamp();
}
Thread.Sleep(1);
}
This will make sure that the logic is executed at specified intervals as long as the system can keep up, so if you try to execute 100 times per second, depending on the logic being executed, the system may not execute this logic 100 times per second. In other cases, this should work fine.
Such logic is good for getting smooth animations that don't accelerate or slow down on different systems, for example.
source
share