How to slow your framerate in XNA / Monogame / FNA

Sometimes, you may need to slow-down your framerate to test certain behavior in your game/app as it would appear on really bad hardware. The following snippet lets you easily tweak a framerate at runtime.

Since Update() gets called every tick, just put this in any GameComponent and you should be good to go. Press “+” to increase the delay (and thus decrease the framerate), or “-” to decrease the delay.

You can comment out this whole method when you’re not using it, or just leave it like it is so that it only builds in DEBUG mode, etc..

#if DEBUG
        static int millisToSleep = 0;
        protected override void Update(GameTime gameTime)
        {
            KeyboardState newKeyState = Keyboard.GetState();
            if (newKeyState.IsKeyDown(Keys.OemPlus))
            {
                millisToSleep += 5;
                // REPLACE THIS WITH WHATEVER SYSTEM YOU USE FOR NOTIFICATIONS: 
                //MessageBoxUtil.Notify("SLEEPING " + millisToSleep + " PER FRAME");
            } else if (newKeyState.IsKeyDown(Keys.OemMinus))
            {
                millisToSleep -= 5;
                //MessageBoxUtil.Notify("SLEEPING " + millisToSleep + " PER FRAME");
            }
            if (millisToSleep > 0)
            {
                System.Threading.Thread.Sleep(millisToSleep);
            }
        }
#endif