-4

I'm trying to create a timing mechanism so that a console application that I'm working on runs at roughly 10 frames a second. When I run this code, rather than being a smooth 20 frames a second it is stuttery and freezes often. I'm having trouble debugging this due to the fact that it relies on the DateTime.Now.Millisecond function, so whenever I insert a break into the code it messes up the timings of the application.

do
{
    frameTimer = DateTime.Now.Millisecond;

    if (firstLoop == true)
    {
        timeTillNextFrame = frameTimer + 100;

        firstLoop = false;
    }

    if (timeTillNextFrame < DateTime.Now.Millisecond && DateTime.Now.Millisecond < 901)
    {
        Console.Clear();

        grid.removeFromGrid(ball.xPos, ball.yPos);
        ball.newPos(gridSizeX, gridSizeY, grid.gridArray);
        grid.addToGrid(ball.xPos, ball.yPos, ball.symbol);

        grid.gridDraw(gridSizeX, gridSizeY);

        timeTillNextFrame = DateTime.Now.Millisecond + 50;

        if (timeTillNextFrame >= 1000)
        {
            timeTillNextFrame -= 1000;
        }

    }
Servy
  • 202,030
  • 26
  • 332
  • 449
  • You probably want `DateTime.Now.TotalMilliseconds` everywhere, right? And I'm not sure what the `< 901` comparison is meant to do. `timeTilNextFrame` also seems like a misleading name. Maybe it should be `timeOfNextFrame`? – user94559 Jun 21 '17 at 13:04
  • 1
    You're seeing the console redrawing itself. You're basically spamming CLS in a command prompt. – Alex Jun 21 '17 at 13:05
  • Why not just use a timer? Also, how long does griddraw take to execute, you need to account for that... – Trey Jun 21 '17 at 13:05
  • I also don't see where/how you wait until the time of the next frame. – user94559 Jun 21 '17 at 13:05
  • (If you do switch to `TotalMilliseconds`, be sure to remove the `if >= 1000 ... -= 1000` code.) – user94559 Jun 21 '17 at 13:06
  • 5
    The console was not meant for this. –  Jun 21 '17 at 13:08
  • @smarx Thank you, I'll make these changes and see how it goes. :) – Jacob Knowles Jun 21 '17 at 13:09
  • @Trey I don't think the grid draw time is a problem but I will take it into account, thank you! – Jacob Knowles Jun 21 '17 at 13:10
  • @Amy No, but I want to try and use it for this. – Jacob Knowles Jun 21 '17 at 13:10
  • Alternatively you could create your own (more or less) emulated console which supports higher refresh rates. Like @Amy said, the console was not made for this purpose. – SharpShade Jun 21 '17 at 13:17
  • Oh, `DateTime.Now.TotalMilliseconds` doesn't exist, sorry. :-) See https://stackoverflow.com/questions/4016483/get-time-in-milliseconds-using-c-sharp for a couple options. `Stopwatch` is probably the best idea (more likely to give you the precision you need). – user94559 Jun 21 '17 at 13:25
  • @Smarx Thanks, using that now. – Jacob Knowles Jun 21 '17 at 13:39
  • Just a random opinion, but your `removeFromGrid` method should just be named `Remove`, and it should have an overload that takes a `Ball` object, so the code would look like: `grid.Remove(ball);` (and the counterpart, `grid.Add(ball);`. Makes for much easier to read (and write) code. – Rufus L Jun 21 '17 at 13:45

1 Answers1

0

You don't want DateTime.Now.Millisecond, you want the total milliseconds:

private static readonly DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

var totalMilliseconds = DateTime.Now.ToUniversalTime().Subtract(Epoch).TotalMilliseconds
nvoigt
  • 75,013
  • 26
  • 93
  • 142