{"record":{"id":"70c853ba1d910705","repo":"dotnet/maui","slug":"uithreadrequired-70c853","errorCode":null,"errorMessage":"UIThreadRequired","messagePattern":"UIThreadRequired","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Compatibility/Core/src/WPF/Interfaces/IContentLoader.cs","lineNumber":20,"sourceCode":"{\n\tusing System;\n\tusing System.Threading;\n\tusing System.Threading.Tasks;\n\tusing System.Windows;\n\n\tpublic interface IContentLoader\n\t{\n\t\tTask<object> LoadContentAsync(FrameworkElement parent, object oldContent, object newContent, CancellationToken cancellationToken);\n\n\t\tvoid OnSizeContentChanged(FrameworkElement parent, object content);\n\t}\n\n\tpublic class DefaultContentLoader : IContentLoader\n\t{\n\t\tpublic Task<object> LoadContentAsync(FrameworkElement parent, object oldContent, object newContent, CancellationToken cancellationToken)\n\t\t{\n\t\t\tif (!Application.Current.Dispatcher.CheckAccess())\n\t\t\t\tthrow new InvalidOperationException(\"UIThreadRequired\");\n\n\t\t\tvar scheduler = TaskScheduler.FromCurrentSynchronizationContext();\n\t\t\treturn Task.Factory.StartNew(() => LoadContent(newContent), cancellationToken, TaskCreationOptions.None, scheduler);\n\t\t}\n\n\t\tprotected virtual object LoadContent(object content)\n\t\t{\n\t\t\tif (content is FrameworkElement)\n\t\t\t\treturn content;\n\n\t\t\tif (content is Uri)\n\t\t\t\treturn Application.LoadComponent(content as Uri);\n\n\t\t\tif (content is string)\n\t\t\t{\n\t\t\t\tif (Uri.TryCreate(content as string, UriKind.RelativeOrAbsolute, out Uri uri))\n\t\t\t\t{\n\t\t\t\t\treturn Application.LoadComponent(uri);","sourceCodeStart":2,"sourceCodeEnd":38,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Compatibility/Core/src/WPF/Interfaces/IContentLoader.cs#L2-L38","documentation":"DefaultContentLoader.LoadContentAsync throws InvalidOperationException(\"UIThreadRequired\") when Application.Current.Dispatcher.CheckAccess() is false. The default loader touches FrameworkElement content and Application.LoadComponent, both of which must run on the WPF dispatcher thread.","triggerScenarios":"Calling LoadContentAsync from a non-UI thread (Task.Run, background thread, ConfigureAwait(false) continuation, native callback); content swap driven by a non-dispatcher scheduler.","commonSituations":"Awaiting IO/network then navigating without dispatcher marshalling; chaining navigation in a Timer.Elapsed handler; multi-window setups where the wrong dispatcher is current.","solutions":["Wrap the load call in Application.Current.Dispatcher.InvokeAsync(...).","Avoid ConfigureAwait(false) on the path that leads to content loading.","Use DispatcherSynchronizationContext for the entire navigation flow.","Add a CheckAccess() guard at the top of your navigation helper to fail with a clearer message."],"exampleFix":"// before\nawait Task.Run(() => loader.LoadContentAsync(parent, old, page, token));\n\n// after\nawait Application.Current.Dispatcher.InvokeAsync(\n    () => loader.LoadContentAsync(parent, old, page, token)).Task.ConfigureAwait(true);","handlingStrategy":"validation","validationCode":"if (!Application.Current.Dispatcher.CheckAccess())\n    await Application.Current.Dispatcher.InvokeAsync(() => loader.LoadContentAsync(parent, old, page, token));","typeGuard":"static bool OnUiThread => System.Windows.Application.Current.Dispatcher.CheckAccess();","tryCatchPattern":"try { await loader.LoadContentAsync(parent, old, page, token); }\ncatch (InvalidOperationException ex) when (ex.Message == \"UIThreadRequired\")\n{ await Application.Current.Dispatcher.InvokeAsync(() => loader.LoadContentAsync(parent, old, page, token)); }","preventionTips":["Use DispatcherSynchronizationContext for the whole navigation flow.","Avoid background-thread content loads.","Add a CheckAccess assertion in navigation entry points."],"tags":["wpf","threading","dispatcher","content-loader","ui-thread"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}