{"record":{"id":"1e7b96f8e202558a","repo":"dotnet/wpf","slug":"new-notsupportedexception-taskextensions","errorCode":null,"errorMessage":"new NotSupportedException()","messagePattern":"new NotSupportedException\\(\\)","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Windows.Presentation/System/Windows/Threading/TaskExtensions.cs","lineNumber":40,"sourceCode":"        {\n            return DispatcherOperationWait(@this, TimeSpan.FromMilliseconds(-1));\n        }\n\n        /// <summary>\n        ///     Waits for the underlying DispatcherOperation to complete.\n        /// </summary>\n        public static DispatcherOperationStatus DispatcherOperationWait(this Task @this, TimeSpan timeout)\n        {\n            var mapping = @this.AsyncState as DispatcherOperationTaskMapping;\n            if(mapping != null)\n            {\n                // This task did come from a DispatcherOperation.\n                return mapping.Operation.Wait(timeout);\n            }\n            else\n            {\n                // This task did not come from a DispatcherOperation.\n                throw new NotSupportedException();\n            }\n        }\n    }\n}\n\n","sourceCodeStart":22,"sourceCodeEnd":46,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Windows.Presentation/System/Windows/Threading/TaskExtensions.cs#L22-L46","documentation":"DispatcherOperationWait throws NotSupportedException when the given Task's AsyncState is not a DispatcherOperationTaskMapping — i.e. the Task did not originate from a DispatcherOperation. The extension method only supports waiting on Tasks created from dispatcher operations; passing any other Task hits this sentinel error.","triggerScenarios":"Thrown at src/Microsoft.DotNet.Wpf/src/System.Windows.Presentation/System/Windows/Threading/TaskExtensions.cs:40 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Only call DispatcherOperationWait on Task objects whose AsyncState is a DispatcherOperationTaskMapping (i.e. tasks created from DispatcherOperation)","Check the AsyncState type before calling and use Task.Wait/await for ordinary tasks","Catch NotSupportedException and fall back to standard task waiting"],"exampleFix":null,"handlingStrategy":"type-guard","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"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"}