{"record":{"id":"87b433d96208af63","repo":"dotnet/maui","slug":"the-dispatcher-was-not-found-and-the-current-appli","errorCode":null,"errorMessage":"The dispatcher was not found and the current application does not have a dispatcher.","messagePattern":"The dispatcher was not found and the current application does not have a dispatcher\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Controls/src/Core/DispatcherExtensions.cs","lineNumber":104,"sourceCode":"\t\t\t\treturn action();\n\t\t\t}\n\t\t}\n\n\t\tstatic IDispatcher EnsureDispatcher(IDispatcher? dispatcher)\n\t\t{\n\t\t\tif (dispatcher is not null)\n\t\t\t\treturn dispatcher;\n\n\t\t\t// maybe this thread has a dispatcher\n\t\t\tif (Dispatcher.GetForCurrentThread() is IDispatcher globalDispatcher)\n\t\t\t\treturn globalDispatcher;\n\n\t\t\t// try looking on the app\n\t\t\tif (Application.Current?.Dispatcher is IDispatcher appDispatcher)\n\t\t\t\treturn appDispatcher;\n\n\t\t\t// no dispatchers found at all\n\t\t\tthrow new InvalidOperationException(\"The dispatcher was not found and the current application does not have a dispatcher.\");\n\t\t}\n\t}\n}\n","sourceCodeStart":86,"sourceCodeEnd":108,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Controls/src/Core/DispatcherExtensions.cs#L86-L108","documentation":"EnsureDispatcher throws InvalidOperationException when the passed dispatcher is null and neither Dispatcher.GetForCurrentThread() nor Application.Current?.Dispatcher yields a dispatcher. This is the guard used by dispatching extension methods to fail fast rather than silently drop a dispatched action.","triggerScenarios":"Calling Dispatch/DispatchIfRequired/etc. with a null dispatcher from a non-UI thread while Application.Current is null or its dispatcher is unset; using Controls in a host with no dispatcher infrastructure.","commonSituations":"Background-thread access to dispatching APIs in test hosts or services; calling extensions before the application dispatcher is created.","solutions":["Capture the dispatcher on the UI thread and pass it explicitly to dispatching calls.","Guard with Dispatcher.GetForCurrentThread() before invoking, and skip work if null in non-UI contexts.","Initialize Application.Current (and thus its dispatcher) before off-thread dispatching."],"exampleFix":"// before\nIDispatcher d = null;\nd.DispatchIfRequired(action); // throws, no dispatcher\n\n// after\nvar d = Dispatcher.GetForCurrentThread();\nif (d is not null) d.DispatchIfRequired(action);\nelse { /* queue or run on main thread */ }","handlingStrategy":"validation","validationCode":"static bool HasAnyDispatcher(IDispatcher? d) =>\n    d is not null\n    || Dispatcher.GetForCurrentThread() is not null\n    || (Application.Current?.Dispatcher is not null);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Capture the dispatcher on the UI thread and pass it explicitly.","Guard calls with Dispatcher.GetForCurrentThread() before dispatching.","Initialize Application.Current before background dispatching."],"tags":["dispatcher","threading","invalidoperation","uithread"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}