WPF Dispatcher {“The calling thread cannot access this object because a different thread owns it.”}

Just in case you want the object to be shared among different threads then always create that object on UI thread. Later when you want to access the object, you can check if you have access to object. If you dont have access, re-invoke the function with UI thread access. example code below:

    private void YourMethod()
    {
        if (Application.Current.Dispatcher.CheckAccess())
        {
            // do whatever you want to do with shared object.
        }
        else
        {
            //Other wise re-invoke the method with UI thread access
            Application.Current.Dispatcher.Invoke(new System.Action(() => YourMethod()));
        }
    }

Leave a Comment