[source lang="csharp"] Stopwatch stopwatch = new Stopwatch(); float timer = (1f / 60f) * 1000000; while (true) { stopwatch.Reset(); stopwatch.Start(); //start update world //end update world Thread.Sleep(1); while (stopwatch.ElapsedTicks < timer) { } Console.WriteLine(stopwatch.ElapsedTicks); stopwatch.Stop(); }[/source]
Its pretty basic, it sleeps for 1 millisecond to prevent 100% CPU usage (well really it sleeps for less than 15-16 milliseconds just because of the way Thread.Sleep works) then uses the stopwatch to see when its time to loop again. It does print out 16667 every cycle which is good, except for some times when it abnormaly printed out 32000+ (32 milliseconds). I am guessing this is due to the point of how Thread.Sleep works. The question is: is the code above good for a completely fixed time-step? should I also make use of winmm.dll (i think thats what it was called) for a more accurate fixed time step?
Also id love to not use Thread.Sleep() but I just cant find another way, the server is console based.
Replies are appriciated, Thanks!
Edited by Xanather, 27 July 2012 - 09:04 AM.







