{"record":{"id":"ee2f4973a00118cb","repo":"dotnet/wpf","slug":"sr-cantshowmodalonnoninteractive","errorCode":null,"errorMessage":"SR.CantShowModalOnNonInteractive","messagePattern":"SR\\.CantShowModalOnNonInteractive","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/Microsoft/Win32/CommonDialog.cs","lineNumber":59,"sourceCode":"        ///  of a common dialog to their default values.\n        /// </summary>\n        public abstract void Reset();\n\n        /// <summary>\n        ///  This is the public method that will be called to actually show\n        ///  a common dialog.  Since CommonDialog is abstract, this function\n        ///  performs initialization tasks for all common dialogs and then\n        ///  calls RunDialog.\n        /// </summary>\n        public virtual bool? ShowDialog()\n        {\n            CheckPermissionsToShowDialog();\n\n            // Don't allow file dialogs to be shown if not in interactive mode\n            // (for example, if we're running as a service)\n            if (!Environment.UserInteractive)\n            {\n                throw new InvalidOperationException(SR.CantShowModalOnNonInteractive);\n            }\n\n            // Call GetActiveWindow to retrieve the window handle to the active window\n            // attached to the calling thread's message queue.  We'll set the owner of\n            // the common dialog to this handle.\n            IntPtr hwndOwner = UnsafeNativeMethods.GetActiveWindow();\n\n            if (hwndOwner == IntPtr.Zero)\n            {\n                // No active window, so we'll use the parking window as the owner, \n                // if its available.\n                if (Application.Current != null)\n                {\n                    hwndOwner = Application.Current.ParkingHwnd;\n                }\n            }\n\n            HwndWrapper tempParentHwnd = null;","sourceCodeStart":41,"sourceCodeEnd":77,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/Microsoft/Win32/CommonDialog.cs#L41-L77","documentation":"CommonDialog.ShowDialog throws this InvalidOperationException when Environment.UserInteractive is false — i.e. the process is running in a non-interactive session such as a Windows Service or a session without a desktop. Modal dialogs require a visible, interactive desktop, so the library refuses to show them rather than hanging or failing deep inside the Win32 common dialog.","triggerScenarios":"Calling ShowDialog() on OpenFileDialog, SaveFileDialog, PrintDialog or any CommonDialog subclass from a Windows Service, a scheduled task in a non-interactive session, an IIS/ASP.NET worker process, or any process where Environment.UserInteractive returns false.","commonSituations":"A service or background worker attempting to prompt the user for a file path; moving working UI code into a service/scheduled job unchanged; CI or headless sessions running UI tests that open file dialogs; print jobs invoked server-side via PrintDialog.","solutions":["Check Environment.UserInteractive before calling ShowDialog and skip/prompt differently when false.","Replace the dialog with a non-interactive configuration path: config file, command-line argument, or settings store for the file path.","Move the dialog into an interactive client (e.g. a UI front end) and keep the service headless, communicating via IPC.","For server-side printing, use non-UI print APIs instead of PrintDialog."],"exampleFix":"// before\nvar dlg = new OpenFileDialog();\nif (dlg.ShowDialog() == true) { ... } // throws in a service\n// after\nif (!Environment.UserInteractive)\n{\n    path = configuration.InputFilePath; // non-interactive source\n}\nelse\n{\n    var dlg = new OpenFileDialog();\n    if (dlg.ShowDialog() == true) { path = dlg.FileName; }\n}","handlingStrategy":"validation","validationCode":"if (Environment.UserInteractive) { /* safe to show dialog */ }","typeGuard":"static bool CanShowDialog() => Environment.UserInteractive;","tryCatchPattern":"try { dlg.ShowDialog(); }\ncatch (InvalidOperationException) { /* non-interactive session: fall back to config */ }","preventionTips":["Check Environment.UserInteractive before any modal dialog","Design services headless: take paths from config instead of dialogs","Keep dialog code in interactive client applications only","Use non-UI APIs for server-side printing"],"tags":["wpf","dialogs","windows-service","non-interactive"],"backgroundTag":"unsupported-operation","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"}