dotnet/wpf · error · InvalidOperationException
SR.Format(SR.CannotCallRunMultipleTimes…
Error message
SR.Format(SR.CannotCallRunMultipleTimes, this.GetType().FullName)
What it means
Application.Run can only execute the application's message loop once. When Run (or Run(Window)) is called after the application has already shut down (_appIsShutdown true), WPF throws InvalidOperationException naming the application type. A single Application instance manages exactly one lifetime.
Solutions
- Call Run exactly once per Application instance, at the entry point (Main/App.g.cs)
- To restart, create a new Application instance (in a fresh process if needed) and Run that
- Check Application.Current == null or _appIsShutdown semantics — track whether Run already returned before calling again
- In tests, build a fresh Application (or avoid Run entirely) per fixture
Example fix
// before app.Run(); // ... later, same instance app.Run(); // throws // after app.Run(); // restart with a new instance var app2 = new MyApp(); app2.Run();
Defensive patterns
Strategy: type-guard
Validate before calling
bool canRun() => Application.Current == null || !Application.Current.Dispatcher.HasShutdownStarted;
Type guard
bool RunAlreadyCompleted(MyApp app) => app.Dispatcher.HasShutdownFinished;
Try / catch
try { app.Run(); } catch (InvalidOperationException) { /* create a new Application instance to restart */ } Prevention
- Call Run once from the app entry point
- Create a new Application to restart
- Never call Run from event handlers
- In tests, instantiate a fresh Application per case
When it happens
Trigger: Calling app.Run() a second time after Shutdown() completed; restarting the loop from Exit handlers; re-invoking Run after Run(Window) returned because the window closed; test code reusing an Application instance across test cases.
Common situations: Attempts to 'restart' an app without creating a new Application; repeated Run calls in loops; unit-test fixtures instantiating one Application but calling Run per test.
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
- SR.Format(SR.BindingExpressionStatusChanged, _status…
- SR.ShutdownModeWhenAppShutdown
- SR.Storyboard_NeverApplied
- SR.UndoNoOpenParentUnit
- SR.UndoUnitOpen
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/0afd20ab4dfd17b8.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Application.cs:1680
EventTrace.EasyTraceEvent(EventTrace.Keyword.KeywordGeneral | EventTrace.Keyword.KeywordPerf, EventTrace.Event.WClientAppRun);
//
// (Can't create app and do run/shutdown followed
// by run/shutdown)
//
// Devs could write the following code
//
// Application app = new Application();
// app.Run();
// app.Run();
//
// In this case, we should throw an exception when Run is called for the second time.
// When app is shutdown, _appIsShutdown is set to true. If it is true here, then we
// throw an exception
if (_appIsShutdown)
{
throw new InvalidOperationException(SR.Format(SR.CannotCallRunMultipleTimes, this.GetType().FullName));
}
if (window != null)
{
if (!window.CheckAccess())
{
throw new ArgumentException(SR.Format(SR.WindowPassedShouldBeOnApplicationThread, window.GetType().FullName, this.GetType().FullName));
}
if (!WindowsInternal.HasItem(window))
{
WindowsInternal.Add(window);
}
if (MainWindow == null)
{
MainWindow = window;
}View on GitHub (pinned to 81131a70a4)