{"record":{"id":"0839fe2da14d7998","repo":"dotnet/maui","slug":"uithreadrequired","errorCode":null,"errorMessage":"UIThreadRequired","messagePattern":"UIThreadRequired","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Compatibility/Core/src/WPF/FormsContentLoader.cs","lineNumber":20,"sourceCode":"using System.Threading;\nusing System.Threading.Tasks;\nusing System.Windows;\nusing Microsoft.Maui.Controls.Compatibility.Platform.WPF.Interfaces;\n\nnamespace Microsoft.Maui.Controls.Compatibility.Platform.WPF\n{\n\tpublic class FormsContentLoader : IContentLoader\n\t{\n\t\tpublic Task<object> LoadContentAsync(FrameworkElement parent, object oldContent, object newContent, CancellationToken cancellationToken)\n\t\t{\n\t\t\tVisualElement element = oldContent as VisualElement;\n\t\t\tif (element != null)\n\t\t\t{\n\t\t\t\telement.Cleanup(); // Cleanup old content\n\t\t\t}\n\n\t\t\tif (!System.Windows.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(parent, newContent), cancellationToken, TaskCreationOptions.None, scheduler);\n\t\t}\n\n\t\tprotected virtual object LoadContent(FrameworkElement parent, object page)\n\t\t{\n\t\t\tVisualElement visualElement = page as VisualElement;\n\t\t\tif (visualElement != null)\n\t\t\t{\n\t\t\t\tvar renderer = CreateOrResizeContent(parent, visualElement);\n\t\t\t\treturn renderer;\n\t\t\t}\n\t\t\treturn null;\n\t\t}\n\n\t\tpublic void OnSizeContentChanged(FrameworkElement parent, object page)\n\t\t{","sourceCodeStart":2,"sourceCodeEnd":38,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Compatibility/Core/src/WPF/FormsContentLoader.cs#L2-L38","documentation":"FormsContentLoader.LoadContentAsync throws InvalidOperationException(\"UIThreadRequired\") when Application.Current.Dispatcher.CheckAccess() returns false, i.e. the call is made on a non-UI thread. WPF content loading and renderer creation require the dispatcher thread because they touch FrameworkElements.","triggerScenarios":"Invoking LoadContentAsync from a Task continuation, background thread, async event handler without marshalling, or a Timer callback; navigation triggered by an awaited network/IO callback that resumed on a thread-pool thread.","commonSituations":"Awaiting without ConfigureAwait(true) on a non-UI context; pushing navigation from a background service; callbacks from native timers; threading violations during page swaps.","solutions":["Marshal the call onto the dispatcher: await Application.Current.Dispatcher.InvokeAsync(() => loader.LoadContentAsync(...)).","Ensure the async chain retains the synchronization context (do not ConfigureAwait(false) before navigation).","Push navigation work through the Forms main/UI thread abstraction if available.","Assert Dispatcher.CheckAccess() at the entry of your navigation helpers to fail early."],"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));","handlingStrategy":"validation","validationCode":"if (!Application.Current.Dispatcher.CheckAccess())\n    await Application.Current.Dispatcher.InvokeAsync(() => LoadAsync());\nelse\n    await LoadAsync();","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":["Marshal navigation onto the dispatcher from non-UI callbacks.","Avoid ConfigureAwait(false) on the navigation path.","Assert CheckAccess at the top of navigation helpers."],"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"}