3

I'm a beginner programmer who learns c# and I wanted to make a text-based snake game in the console window. Now, the game works fairly well but I encounter a problem using the Thread.Sleep() method to pause the code for a specific amount of milliseconds.

The problem is that whenever I try to pause the game for a specific time, it appears to pause for a few milliseconds longer. And since that's a snake game, that's a pretty big problem.

What I tried to do:

while (true)
            {
                double startingTime = DateTime.Now.TimeOfDay.Milliseconds;
                //code
                double lag = DateTime.Now.TimeOfDay.Milliseconds - startingTime;
                Thread.Sleep((int)Math.Round(25 - lag));
                double finalTime = DateTime.Now.TimeOfDay.Milliseconds - startingTime;
                Console.Title = string.Format("Time: {0}", finalTime);
            }

I know for a fact that the code takes about 6 ms to run, so I tried to mesure it and subtract it from the time it supposed to wait between every move. Sometimes it works properly, but more often than not The Console title says it took 31 ms...

I'd really appreciate any suggestion, and if you know a better way to pause the code from running for a precise amount of time, I'll be glad to know :)

SOLVED: I ended up using a stopwatch, like Tamir Suggested, thanks for everyone who helped :D

  • Try using System.Threading.Timer instead of Thread.Sleep due to it's overheads can make difference in your case. Details are here - https://stackoverflow.com/questions/391621/compare-using-thread-sleep-and-timer-for-delayed-execution – Stepan Novikov Oct 19 '17 at 19:24
  • 3
    @StepanNovikov Even a timer isn't going to be precise to within several milliseconds, particularly with such short durations. – Servy Oct 19 '17 at 19:26
  • 5
    If you want to do game development you're really best off using a proper game development framework/engine. If you don't, you're just going to be forced to build that basic framework (of a game loop, etc.) yourself anyway. – Servy Oct 19 '17 at 19:28
  • You are measuring the lag incorrectly by measuring the starting time too late. The starting time should be the time the timer was set to fire, not the time your code got control. So if you set the timer to fire in 30 milliseconds, the start time should be now plus 30 milliseconds, not the time you got around to accessing `DateTime.Now`. If you want to do something every hour, you wait until the *same* number of minutes past the hour before starting the task each time. – David Schwartz Oct 19 '17 at 19:48
  • @Servy Thanks for Answering! I realize this is not the easiest/most efficient way to develop a game, but I thought it would be a fun experiment :P Also, this is the only problem I've encountered thus far, and other than that the game runs perfectly. – Nadav Freedman Oct 19 '17 at 19:48
  • @NadavFreedman And as time goes on, you're going to keep having more and more problems, and it will get harder and harder to fix them. The sooner you switch to a proper paradigm the easier it will be, and the less bad habits you'll develop that you'll need to unlearn. – Servy Oct 19 '17 at 19:52
  • @DavidSchwartz It's frankly irrelevant whether the value is computed correctly or not, because the value is less than the precision of the tool used, so it won't actually change anything. That said, it looks like the time is computed correctly. It computes the difference between the current time and the time it's supposed to continue, it's just that both the current time and the amount of time to wait aren't nearly precise enough to do that. – Servy Oct 19 '17 at 19:57
  • @Servy The time it's supposed to continue is miscalculated. If we're trying to do a task every hour and the last task was supposed to start at 6:00, we need to continue at 7:00. The time the last task actually started is irrelevant and using it is causing errors. – David Schwartz Oct 19 '17 at 20:05

1 Answers1

4

Well, for very precise timing, you should use multimedia timers. Next best choice would be Stopwatch

Tamir
  • 2,503
  • 16
  • 23