dotnet/wpf · error · InvalidOperationException

SR.ApplicationAlreadyRunning

Error message

SR.ApplicationAlreadyRunning

What it means

InvalidOperationException with resource key SR.ApplicationAlreadyRunning is thrown by Application.RunDispatcher when the application's own dispatcher has already been started. WPF's Application may start its message pump (Dispatcher.Run) only once; a second Run/Run(Window) call on the same Application instance while the pump is active is an invalid state and is rejected.

Solutions

  1. Remove the duplicate Application.Run() call; Run should be called exactly once from the entry point.
  2. To restart, call Application.Current.Shutdown(), exit the current process, and launch a new process instead of calling Run again.
  3. Guard the Run call: only invoke it when the dispatcher is not running (Dispatcher.CurrentDispatcher.HasShutdownFinished).
  4. If extra message processing is needed, use Dispatcher.Invoke/BeginInvoke on the existing dispatcher rather than starting a new pump.

Example fix

// before
public static void Main()
{
    var app = new App();
    app.Run();
    app.Run(); // InvalidOperationException: ApplicationAlreadyRunning
}
// after
public static void Main()
{
    var app = new App();
    app.Run(); // called exactly once
}
Defensive patterns

Strategy: validation

Validate before calling

bool canRun = Application.Current == null || Application.Current.Dispatcher == null || Application.Current.Dispatcher.HasShutdownFinished;
if (!canRun) throw new InvalidOperationException("Application dispatcher already running; do not call Run() again.");

Type guard

bool CanCallRun(Application app) => app == null || app.Dispatcher == null || app.Dispatcher.HasShutdownFinished;

Try / catch

try { app.Run(); }
catch (InvalidOperationException ex) when (ex.Message.Contains("already running"))
{
    // dispatcher already active; skip Run
}

Prevention

When it happens

Trigger: Calling Application.Run() or Application.Run(Window) a second time on the same Application instance while the first dispatcher loop is still running, or re-entering Run from code executed on the app's UI thread (e.g. an event handler or Dispatcher callback that calls Run again).

Common situations: Calling app.Run() twice (e.g. in Main and again in a restart path); spawning a second WPF app inside an event handler; attempting to 'restart' the app by calling Run instead of launching a new process; mixing custom Dispatcher.Run() with Application.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 dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/ff1d6b4e7d69e306. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Application.cs:2397

                            {
                                string component = (diff == 1) ? bamlConvertUriSegments[1] : curUriSegments[1];

                                isRootElement = BaseUriHelper.IsComponentEntryAssembly(component);
                            }
                        }
                    }
                }
            }

            return isRootElement;
        }


        private object RunDispatcher(object ignore)
        {
            if (_ownDispatcherStarted)
            {
                throw new InvalidOperationException(SR.ApplicationAlreadyRunning);
            }
            _ownDispatcherStarted = true;
            Dispatcher.Run();
            return null;
        }

        #endregion Private Methods

        //------------------------------------------------------
        //
        //  Private Fields
        //
        //------------------------------------------------------

        #region Private Fields
        private static object                           _globalLock;
        private static bool                             _isShuttingDown;
        private static bool                             _appCreatedInThisAppDomain;

View on GitHub (pinned to 81131a70a4)