I have WPF app with two windows running on different threads (each window has its own thread). First window is basically a splash-screen, and second is main UI of my app.
When i use Live Visual Tree (selecting some element) or Live Property Explorer (searching properties) very often i will get "calling thread cannot access this object" exception. On some debugging sessions i will not get this exception at all.
My guess is that Live Visual Tree and Live Property Explorer somehow using one of my UI threads to get info they use. If they try and use thread of first window (splash-screen) to access visual elements created on second thread, i will get an exception.
Can somebody help me with this?
And please do not suggest using one thread for UI.
Edit:
I found out that most likely this exception occurs when i examine ui elements with some binding. That is - exception happens when Live Visual Tree or Live Property Explorer tries to access data context object, not just examine any property of ui element.
I found a workaround for this problem:
Somewhere in your application you need to subscribe to the following event:
Application.Current.DispatcherUnhandledException += CurrentOnDispatcherUnhandledException;
body of CurrentOnDispatcherUnhandledException can be something like this:
private void CurrentOnDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
e.Handled = true;
if (Debugger.IsAttached
&& (e.Exception?.InnerException?.StackTrace.Contains("System.Windows.Threading.Dispatcher.VerifyAccess()") ?? false)
&& (e.Exception?.InnerException?.StackTrace.Contains("Microsoft.XamlDiagnostics.WpfTap.WpfVisualTreeService.TypeServices.TypeService.GetProperties(Object target)") ?? false))
{
Debug.WriteLine(e.Exception.InnerException.Message);
}
else
ShowExceptionMessage(e.Exception);
}
ShowExceptionMessage is not important here, it can be any error handler you like, or you can even get rid of it (not that i recommend it).
Additionally you can check e.Exception?.InnerException?.Message. I did not included this check here, because message will be language specific.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments