dotnet/wpf · error · ArgumentException

SR.Format(SR.WindowPassedShouldBeOnApplicationThread…

Error message

SR.Format(SR.WindowPassedShouldBeOnApplicationThread, window.GetType().FullName, this.GetType().FullName)

What it means

Application.Run(Window) requires the window to have been created on the same thread as the Application (UI thread). If window.CheckAccess() fails — the window belongs to a different thread — WPF throws ArgumentException naming both the window's and the application's types. WPF objects have thread affinity enforced by DispatcherObject.

Solutions

  1. Create the window on the same (UI) thread that runs Application.Run
  2. Pass null to Run and show the window via its Dispatcher after startup
  3. Ensure the app entry point is [STAThread] and no window construction happens inside Task.Run
  4. Verify with window.Dispatcher == Application.Current.Dispatcher before calling Run(window)

Example fix

// before
var win = Task.Run(() => new MainWindow()).Result;
app.Run(win); // throws
// after
var win = new MainWindow(); // created on UI thread
app.Run(win);
Defensive patterns

Strategy: validation

Validate before calling

bool windowOnAppThread(Window w) => w != null && w.Dispatcher == Application.Current.Dispatcher;

Type guard

bool CanRunWindow(Window w) => w is not null && w.CheckAccess();

Try / catch

try { app.Run(win); } catch (ArgumentException) { win.Dispatcher.Invoke(() => win.Show()); app.Run(); }

Prevention

When it happens

Trigger: Calling app.Run(someWindow) where someWindow was constructed on a background/worker thread; creating the main window inside a Task.Run and handing it to Run on the main thread; MTA scenarios with multiple UI threads.

Common situations: Splash screens or windows created on worker threads; async window factory methods capturing the wrong dispatcher; mixing STA threads in multi-window apps.

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

Appendix: source

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

            // 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;
                }

                if (window.Visibility != Visibility.Visible)
                {
                    Dispatcher.BeginInvoke(
                        DispatcherPriority.Send,
                        (DispatcherOperationCallback) delegate(object obj)
                        {

View on GitHub (pinned to 81131a70a4)