3

I'm just wondering, while making a game, I noticed that the GameTime value for IsRunningSlowly returns false when my game has focus (like it should), but when I change applications, it changes to true. I even made a empty game, and even when it losses focus, the GameTime value for IsRunningSlowly returns true as well. I'm wondering why does it do this? Is it just my computer, or did the creators of XNA design it this way? The frame rate seems fine, but the value is true. No big deal really, I'm just really curious!

[Empty Game]

    public class Game1 : Microsoft.Xna.Framework.Game
{
    #region Constuctors

    public Game1()
    {
        this.GraphicsManager = new Microsoft.Xna.Framework.GraphicsDeviceManager(this);
        this.Content.RootDirectory = "Content";
    }

    #endregion
    #region Overrides

    protected override void LoadContent()
    {
        this.SpriteBatch = new Microsoft.Xna.Framework.Graphics.SpriteBatch(this.GraphicsDevice);
        base.LoadContent();
    }
    protected override void Update(Microsoft.Xna.Framework.GameTime GameTime)
    {
        System.Console.WriteLine(GameTime.IsRunningSlowly);
        Microsoft.Xna.Framework.Input.KeyboardState Keyboard = Microsoft.Xna.Framework.Input.Keyboard.GetState();
        if (Keyboard.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Escape)) this.Exit();
        base.Update(GameTime);
    }
    protected override void Draw(Microsoft.Xna.Framework.GameTime gameTime)
    {
        GraphicsDevice.Clear(Microsoft.Xna.Framework.Color.CornflowerBlue);
        base.Draw(gameTime);
    }

    #endregion
    #region Variables

    private Microsoft.Xna.Framework.GraphicsDeviceManager GraphicsManager { get; set; }
    private Microsoft.Xna.Framework.Graphics.SpriteBatch SpriteBatch { get; set; }

    #endregion
}

2 Answers2

7

I'll try and answer the question, and not post a "this is just how it is". The XNA thread sleeps when it loses focus. It is configured in XNA's game class.

InactiveSleepTime = new TimeSpan(0);

It's set to 20ms by default, and setting it to 0 will make XNA run full speed while it has lost focus.

IsRunningSlowly gets set to true since the framerate drops below 60 (which is the default when IsFixedTimeStep is set)

Sasha Carlson
  • 71
  • 1
  • 2
2

This is by design. The games are one of the most resource consuming kinds of applications out there, so naturally XNA framework developers thought it might be a good idea to drop some frames when the game is not in focus to save some CPU for other operations you might want to do while the game is in background.

XNA game also doesn't make Draw calls when its main window is minimized. I believe the Update calls are never dropped (though I may be wrong).

user1306322
  • 8,561
  • 18
  • 61
  • 122
  • It seems to drop `Update`s when you move or resize the window. (I'm currently wrestling with this issue - haven't verified it 100% yet.) – Andrew Russell Feb 23 '13 at 06:40
  • @AndrewRussell yes, I've encountered that feature before. Game seems to drop all `Update` calls when moving and resizing the game window. Couldn't say it's a big problem for me, but it might become in future when I add multiplayer support and connection timeout for reliability. My guess is that this may be countered by (worst case scenario) running the game in a wrapper application, as I hope the parent window won't forward "being resized/moved" calls to its child game window. Also you might wanna try a non-game window for resizing and moving and borderless (real) game window that auto-fits it – user1306322 Feb 23 '13 at 08:39
  • Mmm - pretty much exactly the problem I'm having. It stops pumping network events and desynchronises network timing. Most irritating. Seems it's a win32 thing ([good description](http://www.gamedev.net/topic/488074-win32-message-pump-and-opengl---rendering-pauses-while-draggingresizing/?view=findpost&p=4189300)). – Andrew Russell Feb 23 '13 at 10:03