[WinUI 3] COMException: The application called an interface that was marshalled for a different thread. #8410
-
Hello, Source Code: |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
The result of the GetMessage() is coming back on a non-UI thread, meaning PropertyChanged is invoked on a non-UI thread. GPT-4 can handle the rest: In WinUI or UWP, you cannot invoke PropertyChanged on a non-UI thread because the UI elements can only be accessed from the UI thread. If you attempt to access UI elements from a non-UI thread, you may encounter unexpected behavior or crashes in your application. To update UI elements from a non-UI thread, you can use the Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() =>
{
// update your UI here
Update(i);
}); For WinUI 3, you can use the this.DispatcherQueue.TryEnqueue(() =>
{
// update your UI here
Update(i);
}); It's worth noting that in WinUI 3, the CoreDispatcher has been succeeded by DispatcherQueue. Most UI objects in WinUI 3 now have a DispatcherQueue property instead of a CoreDispatcher property. You can access the DispatcherQueue from any UI element or Window Source 4. In summary, when updating UI elements from a non-UI thread, you should use |
Beta Was this translation helpful? Give feedback.
The result of the GetMessage() is coming back on a non-UI thread, meaning PropertyChanged is invoked on a non-UI thread.
GPT-4 can handle the rest:
In WinUI or UWP, you cannot invoke PropertyChanged on a non-UI thread because the UI elements can only be accessed from the UI thread. If you attempt to access UI elements from a non-UI thread, you may encounter unexpected behavior or crashes in your application.
To update UI elements from a non-UI thread, you can use the
Dispatcher.RunAsync
method in UWP orDispatcherQueue.TryEnqueue
method in WinUI 3. Here's an example of how to useDispatcher.RunAsync
in UWP: