stride3d/stride · error · InvalidOperationException

Cannot run this instance while it is already running

Error message

Cannot run this instance while it is already running

What it means

GameBase.Run() throws this InvalidOperationException if the game instance has already been started and has not yet exited. The Stride game loop is single-instance per Game object, so calling Run() twice on the same instance is an invalid state transition. The guard is IsRunning, set during Run and cleared when the game exits.

Solutions

  1. Create a new instance of the Game class instead of re-running the same one
  2. Check game.IsRunning before calling Run and skip if already running
  3. If the game exited unexpectedly, call Dispose on the old instance and construct a fresh Game
  4. Move Run out of loops/callbacks so it is only called once from the main entry point

Example fix

// before
if (shouldRestart) { game.Run(); }
// after
if (shouldRestart) { game.Dispose(); game = new MyGame(); game.Run(); }
Defensive patterns

Strategy: validation

Validate before calling

if (!game.IsRunning) game.Run(gameContext);

Type guard

bool CanRun(Game game) => game is { IsRunning: false };

Try / catch

try { game.Run(); } catch (InvalidOperationException ex) when (ex.Message.Contains("already running")) { /* skip: instance active */ }

Prevention

When it happens

Trigger: Calling game.Run(gameContext) twice on the same Game instance, e.g. calling Run again from an event handler, a loop, or after an unhandled frame exception left IsRunning true.

Common situations: Restarting a game without creating a new Game instance; calling Run in both initialization code and a callback; hosting code that retries Run after a crash; script/hot-reload systems re-invoking Run.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/7f8439afed39b3ef. Report an issue: GitHub.

Appendix: source

Thrown at sources/engine/Stride.Games/GameBase.cs:420

                }
            }
            catch (Exception ex)
            {
                Log.Error("Unexpected exception", ex);
                throw;
            }
        }

        /// <summary>
        /// Call this method to initialize the game, begin running the game loop, and start processing events for the game.
        /// </summary>
        /// <param name="gameContext">The window Context for this game.</param>
        /// <exception cref="System.InvalidOperationException">Cannot run this instance while it is already running</exception>
        public void Run(GameContext gameContext = null)
        {
            if (IsRunning)
            {
                throw new InvalidOperationException("Cannot run this instance while it is already running");
            }

            // Gets the graphics device manager
            graphicsDeviceManager = Services.GetService<IGraphicsDeviceManager>();
            if (graphicsDeviceManager == null)
            {
                throw new InvalidOperationException("No GraphicsDeviceManager found");
            }

            // Gets the GameWindow Context
            if (gameContext == null)
            {
                AppContextType c;
                if (OperatingSystem.IsWindows() && GameContextFactory.WinFormsBackendEnabled)
                    c = AppContextType.DesktopWinForms;
                else if (OperatingSystem.IsAndroid())
                    c = AppContextType.Android;
                else if (OperatingSystem.IsIOS() || OperatingSystem.IsTvOS() || OperatingSystem.IsWatchOS())

View on GitHub (pinned to 96fad776d2)