{"record":{"id":"fdb8114116d2ff62","repo":"dotnet/wpf","slug":"sr-format-sr-windowpassedshouldbeonapplicationthread-window","errorCode":null,"errorMessage":"SR.Format(SR.WindowPassedShouldBeOnApplicationThread, window.GetType().FullName, this.GetType().FullName)","messagePattern":"SR\\.Format\\(SR\\.WindowPassedShouldBeOnApplicationThread, window\\.GetType\\(\\)\\.FullName, this\\.GetType\\(\\)\\.FullName\\)","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Application.cs","lineNumber":1687,"sourceCode":"            // Devs could write the following code\n            //\n            // Application app = new Application();\n            // app.Run();\n            // app.Run();\n            //\n            // In this case, we should throw an exception when Run is called for the second time.\n            // When app is shutdown, _appIsShutdown is set to true.  If it is true here, then we\n            // throw an exception\n            if (_appIsShutdown)\n            {\n                throw new InvalidOperationException(SR.Format(SR.CannotCallRunMultipleTimes, this.GetType().FullName));\n            }\n\n            if (window != null)\n            {\n                if (!window.CheckAccess())\n                {\n                    throw new ArgumentException(SR.Format(SR.WindowPassedShouldBeOnApplicationThread, window.GetType().FullName, this.GetType().FullName));\n                }\n\n                if (!WindowsInternal.HasItem(window))\n                {\n                    WindowsInternal.Add(window);\n                }\n\n                if (MainWindow == null)\n                {\n                    MainWindow = window;\n                }\n\n                if (window.Visibility != Visibility.Visible)\n                {\n                    Dispatcher.BeginInvoke(\n                        DispatcherPriority.Send,\n                        (DispatcherOperationCallback) delegate(object obj)\n                        {","sourceCodeStart":1669,"sourceCodeEnd":1705,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Application.cs#L1669-L1705","documentation":"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.","triggerScenarios":"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.","commonSituations":"Splash screens or windows created on worker threads; async window factory methods capturing the wrong dispatcher; mixing STA threads in multi-window apps.","solutions":["Create the window on the same (UI) thread that runs Application.Run","Pass null to Run and show the window via its Dispatcher after startup","Ensure the app entry point is [STAThread] and no window construction happens inside Task.Run","Verify with window.Dispatcher == Application.Current.Dispatcher before calling Run(window)"],"exampleFix":"// before\nvar win = Task.Run(() => new MainWindow()).Result;\napp.Run(win); // throws\n// after\nvar win = new MainWindow(); // created on UI thread\napp.Run(win);","handlingStrategy":"validation","validationCode":"bool windowOnAppThread(Window w) => w != null && w.Dispatcher == Application.Current.Dispatcher;","typeGuard":"bool CanRunWindow(Window w) => w is not null && w.CheckAccess();","tryCatchPattern":"try { app.Run(win); } catch (ArgumentException) { win.Dispatcher.Invoke(() => win.Show()); app.Run(); }","preventionTips":["Create windows on the application's UI thread","Keep [STAThread] on Main","Avoid constructing windows inside Task.Run","Compare Dispatchers before Run(window)"],"tags":["wpf","threading","dispatcher","window","thread-affinity"],"backgroundTag":"invalid-state-transition","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}