{"record":{"id":"db99388e5df092a8","repo":"stride3d/stride","slug":"invoking-shouldstayopen-before-the-work-is-finished","errorCode":null,"errorMessage":"Invoking ShouldStayOpen before the work is finished.","messagePattern":"Invoking ShouldStayOpen before the work is finished\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/editor/Stride.Core.Assets.Editor/ViewModel/Progress/WorkProgressViewModel.cs","lineNumber":204,"sourceCode":"        public void RegisterProgressStatus(IProgressStatus progressStatus, bool updateAsync)\n        {\n            registeredProgressStatus = progressStatus;\n            progressStatusUpdateAsync = updateAsync;\n            progressStatus.ProgressChanged += ProgressChanged;\n            Minimum = 0;\n            Maximum = 1;\n        }\n\n        /// <summary>\n        /// Indicates whether the progress window should stay open.\n        /// This method throws an exception if invoked when <see cref=\"WorkDone\"/> is false.\n        /// </summary>\n        /// <returns><c>true</c> if the window should stay open, <c>false</c> otherwise.</returns>\n        public bool ShouldStayOpen()\n        {\n            if (!WorkDone)\n            {\n                throw new InvalidOperationException(\"Invoking ShouldStayOpen before the work is finished.\");\n            }\n\n            switch (KeepOpen)\n            {\n                case KeepOpen.Never:\n                    return false;\n                case KeepOpen.OnWarningsOrErrors:\n                    return Log.HasWarnings || Log.HasErrors;\n                case KeepOpen.OnErrors:\n                    return Log.HasErrors;\n                case KeepOpen.Always:\n                    return true;\n                default:\n                    throw new ArgumentOutOfRangeException();\n            }\n        }\n\n        internal void NotifyWindowWillOpen()","sourceCodeStart":186,"sourceCodeEnd":222,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/editor/Stride.Core.Assets.Editor/ViewModel/Progress/WorkProgressViewModel.cs#L186-L222","documentation":"WorkProgressViewModel.ShouldStayOpen() decides whether the progress window should remain visible after a work item completes. The library throws InvalidOperationException if called before WorkDone is true, because the keep-open decision is only meaningful for finished work. It is a guard against querying an unfinished progress state.","triggerScenarios":"Calling ShouldStayOpen() while the associated work is still executing (WorkDone == false), typically from UI code that polls the window state too early or before awaiting the work's completion.","commonSituations":"A developer wires the progress dialog close logic in the window's Closing handler while the background task is still running; or an editor plugin checks keep-open behavior immediately after ShowProgressWindow without waiting for the operation to finish.","solutions":["Ensure ShouldStayOpen() is only invoked after WorkDone becomes true (e.g. await the work task before querying).","Move the call into the work-completed callback/continuation instead of polling.","Guard the call with an if (vm.WorkDone) check and defer otherwise.","Catch InvalidOperationException and treat it as 'not ready yet' if early calls are unavoidable."],"exampleFix":"// before\nif (!progressViewModel.ShouldStayOpen())\n    window.Close();\n// after\nawait workTask;\nif (progressViewModel.WorkDone && !progressViewModel.ShouldStayOpen())\n    window.Close();","handlingStrategy":"validation","validationCode":"if (!progressViewModel.WorkDone) throw new InvalidOperationException(\"Work is not finished yet; defer ShouldStayOpen\");\nvar stayOpen = progressViewModel.ShouldStayOpen();","typeGuard":null,"tryCatchPattern":"try { stayOpen = vm.ShouldStayOpen(); }\ncatch (InvalidOperationException) { stayOpen = true; /* window stays until work done */ }","preventionTips":["Only call ShouldStayOpen from the work-completed continuation","Never poll ShouldStayOpen while work is running","Check WorkDone before any keep-open logic"],"tags":["csharp","viewmodel","invalid-state"],"backgroundTag":"invalid-state-transition","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}