stride3d/stride · error · InvalidOperationException

This application already has a main window.

Error message

This application already has a main window.

What it means

WindowManager allows only one main window per application. ShowMainWindow throws InvalidOperationException when MainWindow is already set, because assigning a second main window would break owner/window-management bookkeeping. The error is logged before throwing.

Solutions

  1. Only call ShowMainWindow once, at application startup
  2. Check WindowManager.MainWindow != null before calling ShowMainWindow and reuse/activate the existing window instead
  3. Close and reset the existing main window (via WindowManager APIs) before showing a new one
  4. Guard the call with a once-only initialization flag in your app bootstrap

Example fix

// before
windowManager.ShowMainWindow(mainWindow);
// later
windowManager.ShowMainWindow(newMainWindow); // throws
// after
if (windowManager.MainWindow == null)
    windowManager.ShowMainWindow(newMainWindow);
else
    windowManager.MainWindow.Window.Activate();
Defensive patterns

Strategy: validation

Validate before calling

if (windowManager.MainWindow == null) windowManager.ShowMainWindow(window); else windowManager.MainWindow.Window.Activate();

Try / catch

try { windowManager.ShowMainWindow(window); } catch (InvalidOperationException) { /* main window already exists; activate it instead */ }

Prevention

When it happens

Trigger: Calling ShowMainWindow twice in one application lifetime, e.g. re-invoking it on a second startup path or after re-creating a window without closing/unregistering the first.

Common situations: App restart logic that shows a new shell window; plugin or module code that independently tries to register its window as main; unit tests that call ShowMainWindow without resetting WindowManager state between tests.

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/eba8e81f5564b1a1. Report an issue: GitHub.

Appendix: source

Thrown at sources/presentation/Stride.Core.Presentation.Wpf/Windows/WindowManager.cs:104

            Logger.Info($"{nameof(WindowManager)} disposed");
            initialized = false;
        }

        /// <summary>
        /// Shows the given window as the main window of the application. It is mandatory to use this method for the main window to use features of the <see cref="WindowManager"/>.
        /// </summary>
        /// <param name="window">The main window to show.</param>
        public static void ShowMainWindow([NotNull] Window window)
        {
            if (window == null) throw new ArgumentNullException(nameof(window));
            CheckDispatcher();

            if (MainWindow != null)
            {
                var message = "This application already has a main window.";
                Logger.Error(message);
                throw new InvalidOperationException(message);
            }
            Logger.Info($"Main window showing. ({window})");

            MainWindow = new WindowInfo(window);
            AllWindowsList.Add(MainWindow);

            window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            window.Show();
        }

        /// <summary>
        /// Shows the given window as blocking window. A blocking window will always block the main window of the application, even if shown before it, but does not
        /// affect modal windows. However it can still be blocked by them..
        /// </summary>
        /// <param name="window">The blocking window to show.</param>
        public static void ShowBlockingWindow([NotNull] Window window)
        {
            if (window == null) throw new ArgumentNullException(nameof(window));

View on GitHub (pinned to 96fad776d2)