"calling thread cannot access this object" exception when working with live visual tree in multithreaded UI WPF app

Max

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.

Max

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

"The calling thread cannot access this object because a different thread owns it" error when updating UI control from different thread in WPF

From Dev

the calling thread cannot access this object because a different thread owns it wpf

From Dev

The Calling thread cannot access this object because a different thread owns it,WPF

From Dev

WPF: The calling thread cannot access this object because a different thread owns it

From Dev

ReactiveUI WPF - The calling thread cannot access this object because a different thread owns it

From Dev

WPF error 'calling thread cannot access this object' trying to play sounds in c# class

From Dev

The calling thread cannot access this object because a different thread owns it, even when using the dispatcher

From Dev

Calling thread cannot access object when it's created from the same thread

From Dev

Getting error The calling thread cannot access this object because a different thread owns it wpf, How to use Dispatch.invovef

From Dev

Famous the calling thread cannot access this object because a different issue

From

The calling thread cannot access this object because a different thread owns it

From Dev

Calling thread cannot access object due to separate thread ownership

From Dev

The calling thread cannot access this object because a different thread owns it (with Dispatcher)

From Dev

How to use Live Visual Tree on WPF window within a WinForms application?

From Dev

The calling thread cannot access this object even after adding Dispatcher.Invoke

From Dev

The calling thread cannot access this object because a different thread owns it - BackgroundWorker error

From Dev

Error:The calling thread cannot access this object because a different thread owns it. Storyboard simulation

From Dev

c# - Changes in cefsharp 79.1.35 (from 75.1.143) creates: 'The calling thread cannot access this object because a different thread owns it.'

From Dev

WPF app crashes when using Visual States

From Dev

Live Visual Tree Without StartupUri

From Dev

InstallReferrerReceiver not working in live app

From Dev

Which control has the focus in my WPF window, how to find this in Live Visual Tree?

From Dev

Unable to enable VS 2019 Live Visual Tree. No option for "Enable UI Debugging Tools"

From Dev

How to disable frame rate counter and live visual tree toolbar when debugging UWP?

From

How to remove the "Go to live visual tree" / "Enable selection" / "Display layout adorners" overlay when debugging?

From Dev

mobile app working with live calls

From Dev

How to interact with UI elements when using MVVM/MVVMLight in a WPF app

From Dev

Whole blazor web app stop working when exception occured

From Dev

styling not working on mobile when live

Related Related

  1. 1

    "The calling thread cannot access this object because a different thread owns it" error when updating UI control from different thread in WPF

  2. 2

    the calling thread cannot access this object because a different thread owns it wpf

  3. 3

    The Calling thread cannot access this object because a different thread owns it,WPF

  4. 4

    WPF: The calling thread cannot access this object because a different thread owns it

  5. 5

    ReactiveUI WPF - The calling thread cannot access this object because a different thread owns it

  6. 6

    WPF error 'calling thread cannot access this object' trying to play sounds in c# class

  7. 7

    The calling thread cannot access this object because a different thread owns it, even when using the dispatcher

  8. 8

    Calling thread cannot access object when it's created from the same thread

  9. 9

    Getting error The calling thread cannot access this object because a different thread owns it wpf, How to use Dispatch.invovef

  10. 10

    Famous the calling thread cannot access this object because a different issue

  11. 11

    The calling thread cannot access this object because a different thread owns it

  12. 12

    Calling thread cannot access object due to separate thread ownership

  13. 13

    The calling thread cannot access this object because a different thread owns it (with Dispatcher)

  14. 14

    How to use Live Visual Tree on WPF window within a WinForms application?

  15. 15

    The calling thread cannot access this object even after adding Dispatcher.Invoke

  16. 16

    The calling thread cannot access this object because a different thread owns it - BackgroundWorker error

  17. 17

    Error:The calling thread cannot access this object because a different thread owns it. Storyboard simulation

  18. 18

    c# - Changes in cefsharp 79.1.35 (from 75.1.143) creates: 'The calling thread cannot access this object because a different thread owns it.'

  19. 19

    WPF app crashes when using Visual States

  20. 20

    Live Visual Tree Without StartupUri

  21. 21

    InstallReferrerReceiver not working in live app

  22. 22

    Which control has the focus in my WPF window, how to find this in Live Visual Tree?

  23. 23

    Unable to enable VS 2019 Live Visual Tree. No option for "Enable UI Debugging Tools"

  24. 24

    How to disable frame rate counter and live visual tree toolbar when debugging UWP?

  25. 25

    How to remove the "Go to live visual tree" / "Enable selection" / "Display layout adorners" overlay when debugging?

  26. 26

    mobile app working with live calls

  27. 27

    How to interact with UI elements when using MVVM/MVVMLight in a WPF app

  28. 28

    Whole blazor web app stop working when exception occured

  29. 29

    styling not working on mobile when live

HotTag

Archive