{"record":{"id":"7f8439afed39b3ef","repo":"stride3d/stride","slug":"cannot-run-this-instance-while-it-is-already-running","errorCode":null,"errorMessage":"Cannot run this instance while it is already running","messagePattern":"Cannot run this instance while it is already running","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/engine/Stride.Games/GameBase.cs","lineNumber":420,"sourceCode":"                }\n            }\n            catch (Exception ex)\n            {\n                Log.Error(\"Unexpected exception\", ex);\n                throw;\n            }\n        }\n\n        /// <summary>\n        /// Call this method to initialize the game, begin running the game loop, and start processing events for the game.\n        /// </summary>\n        /// <param name=\"gameContext\">The window Context for this game.</param>\n        /// <exception cref=\"System.InvalidOperationException\">Cannot run this instance while it is already running</exception>\n        public void Run(GameContext gameContext = null)\n        {\n            if (IsRunning)\n            {\n                throw new InvalidOperationException(\"Cannot run this instance while it is already running\");\n            }\n\n            // Gets the graphics device manager\n            graphicsDeviceManager = Services.GetService<IGraphicsDeviceManager>();\n            if (graphicsDeviceManager == null)\n            {\n                throw new InvalidOperationException(\"No GraphicsDeviceManager found\");\n            }\n\n            // Gets the GameWindow Context\n            if (gameContext == null)\n            {\n                AppContextType c;\n                if (OperatingSystem.IsWindows() && GameContextFactory.WinFormsBackendEnabled)\n                    c = AppContextType.DesktopWinForms;\n                else if (OperatingSystem.IsAndroid())\n                    c = AppContextType.Android;\n                else if (OperatingSystem.IsIOS() || OperatingSystem.IsTvOS() || OperatingSystem.IsWatchOS())","sourceCodeStart":402,"sourceCodeEnd":438,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/engine/Stride.Games/GameBase.cs#L402-L438","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Create a new instance of the Game class instead of re-running the same one","Check game.IsRunning before calling Run and skip if already running","If the game exited unexpectedly, call Dispose on the old instance and construct a fresh Game","Move Run out of loops/callbacks so it is only called once from the main entry point"],"exampleFix":"// before\nif (shouldRestart) { game.Run(); }\n// after\nif (shouldRestart) { game.Dispose(); game = new MyGame(); game.Run(); }","handlingStrategy":"validation","validationCode":"if (!game.IsRunning) game.Run(gameContext);","typeGuard":"bool CanRun(Game game) => game is { IsRunning: false };","tryCatchPattern":"try { game.Run(); } catch (InvalidOperationException ex) when (ex.Message.Contains(\"already running\")) { /* skip: instance active */ }","preventionTips":["Call Run only once from the application entry point","Create a fresh Game instance for each game session","Track lifecycle with your own started flag in hosting code"],"tags":["game-loop","invalid-state","stride"],"backgroundTag":"invalid-state-transition","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}