Search code examples
c#visual-studio-2022monogame

Visual Studio 2022 has hang-ups while Running code


I wrote a little program in C#/Monogame which allows me to move a piece on a chess board by keyboard commands. One tile at a time.

While testing the code I noticed, that sometimes the program is not receptive to the keyboard for several seconds. When the program comes back to live, it doesn't play all the missed inputs, but instead pretend as if nothing happened.

In order to figure it out, I put a printline command into the update method like this:

    public override void Update(GameTime gameTime)
    {
        if (Globals.WorldTime_s - lastSlowUpdate >= 1)//once per second
        {
            lastSlowUpdate = Globals.WorldTime_s;
            Debug.WriteLine($"PlayerTurn: {lastSlowUpdate}");
        }
    }

The world time is updated like this in the Globals class, which is static:

    public static void Update(GameTime gameTime)
    {
        _worldTime_s += gameTime.ElapsedGameTime.TotalSeconds;
    }

What I see is the following: for 5 seconds or so I get a printLine every second with timestamps spaced by 1 second, as expected. Then nothing happens for like 2 or 3 seconds until the next printLine comes. The next timestamp is the last timestamp plus 1 second. In this gap the sw will not respond to the keyboard either.

I believe the problem is in Visual Studio (2022, Version 17.10.4), as the code is doing what it should.

Is this normal, even for my little "Hello world" toy program? How can I improve this? Testing the software becomes difficult if it doesn't respond reliably to the keyboard.


Solution

  • I found my problem. The solution was to change

    public void Draw(GameTime gameTime)
    {
        for (int x = 0; x < MapSize.X; x++)
        {
            for (int y = 0; y < MapSize.Y; ++y)
            {
                Color color;
                if (tiles[x][y].collision) { color = ColorWall; }
                else { color = ColorOpen; }
    
                Texture2D texture = new Texture2D(Globals.GraphicsDevice, 1, 1);
                texture.SetData<Color>(new Color[] { color });
                Rectangle targetRect = new Rectangle(x * TileSize + 1, y * TileSize + 1, TileSize - 2, TileSize - 2);
                Rectangle sourceRect = new Rectangle(0, 0, 1, 1);
                Globals.SpriteBatch.Draw(texture, targetRect, sourceRect, Color.White);
            }
        }
    

    into

    public void Draw(GameTime gameTime)
    {
        Texture2D texture = new Texture2D(Globals.GraphicsDevice, 1, 1);
    
        for (int x = 0; x < MapSize.X; x++)
        {
            for (int y = 0; y < MapSize.Y; ++y)
            {
                Color color;
                if (tiles[x][y].collision) { color = ColorWall; }
                else { color = ColorOpen; }
    
                
                texture.SetData<Color>(new Color[] { color });
                Rectangle targetRect = new Rectangle(x * TileSize + 1, y * TileSize + 1, TileSize - 2, TileSize - 2);
                Rectangle sourceRect = new Rectangle(0, 0, 1, 1);
                Globals.SpriteBatch.Draw(texture, targetRect, sourceRect, Color.White);
            }
        }
    
    }
    

    The declaration of texture is now outside of the loop.