{"record":{"id":"8c9a90b505e8d470","repo":"dotnet/wpf","slug":"the-operation-has-timed-out","errorCode":null,"errorMessage":"The operation has timed out.","messagePattern":"The operation has timed out\\.","errorType":"exception","errorClass":"TimeoutException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Threading/Dispatcher.cs","lineNumber":1381,"sourceCode":"                    // This should not block because either the operation\n                    // is using the old async sematics, or the operation\n                    // completed successfully.\n                    result = operation.Result;\n                }\n                catch(OperationCanceledException)\n                {\n                    Debug.Assert(operation.Status == DispatcherOperationStatus.Aborted);\n\n                    // New async semantics will throw an exception if the\n                    // operation was aborted.  Here we convert that\n                    // exception into a timeout exception if the timeout\n                    // has expired (admittedly a weak relationship\n                    // assuming causality).\n                    if (ctTimeout.IsCancellationRequested)\n                    {\n                        // The operation was canceled because of the\n                        // timeout, throw a TimeoutException instead.\n                        throw new TimeoutException();\n                    }\n                    else\n                    {\n                        // The operation was canceled from some other reason.\n                        throw;\n                    }\n                }\n                finally\n                {\n                    ctTimeoutRegistration.Dispose();\n                    ctsTimeout?.Dispose();\n                }\n            }\n\n            return result;\n        }\n\n        /// <summary>","sourceCodeStart":1363,"sourceCodeEnd":1399,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Threading/Dispatcher.cs#L1363-L1399","documentation":"Dispatcher.InvokeImpl waits for the invoked operation to complete under a CancellationToken tied to the timeout. If the wait ends because the cancellation token fired due to the timeout — and not because of an external cancellation — the dispatcher throws TimeoutException with the default message 'The operation has timed out.' This distinguishes a genuine timeout from a user-requested cancellation, which is rethrown as-is.","triggerScenarios":"Calling Dispatcher.Invoke (synchronous, cross-thread) where the dispatched operation cannot run within the specified timeout because the target dispatcher thread is busy/blocked with higher-priority or long-running work, or is itself deadlocked.","commonSituations":"Invoke from a worker thread onto a UI thread that is blocked waiting on the worker (classic deadlock until timeout); rendering/layout storms keeping the UI thread busy past the timeout; timeouts set too short (e.g. seconds) for heavy work.","solutions":["Increase the timeout, or pass TimeSpan.FromMilliseconds(-1) to wait indefinitely when acceptable.","Avoid cross-thread Invoke when the target thread might wait on the caller (deadlock); use BeginInvoke/InvokeAsync (fire-and-forget) or marshal results with await.","Catch TimeoutException at the call site and decide on retry/abort behavior.","Ensure the UI thread's dispatcher queue is not starved (avoid 100+ ms synchronous work on it)."],"exampleFix":"// before\ndispatcher.Invoke(() => Compute(), DispatcherPriority.Normal, TimeSpan.FromSeconds(1));\n// after\nvar op = dispatcher.BeginInvoke(() => Compute());\n// do not block the worker on the UI thread; continue asynchronously\nop.Completed += (s, e) => { /* use op.Result */ };","handlingStrategy":"try-catch","validationCode":"if (Dispatcher.FromThread(targetThread) == null || targetThread.IsAlive == false)\n    return; // do not block on a dead/absent dispatcher\ndispatcher.Invoke(work, DispatcherPriority.Send, TimeSpan.FromSeconds(30));","typeGuard":null,"tryCatchPattern":"try\n{\n    dispatcher.Invoke(work, DispatcherPriority.Normal, TimeSpan.FromSeconds(30));\n}\ncatch (TimeoutException)\n{\n    // UI thread busy or deadlocked; retry with a longer timeout or go async\n    dispatcher.BeginInvoke(work);\n}","preventionTips":["Never call blocking Invoke onto a thread that may itself be waiting on you (deadlock)","Use BeginInvoke/InvokeAsync with await instead of synchronous cross-thread Invoke","Give timeouts generous headroom for busy UI threads","Keep UI-thread work slices short so dispatched items run promptly"],"tags":["wpf","dispatcher","timeout","deadlock"],"backgroundTag":"request-timeout","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"}