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

14,014

Solution 1

You want to look into the Dispatcher.Invoke method.

Solution 2

You could use the BackgroundWorker class to perform your asynchronous operations; it should take care of any thread affinity issues you might be having. It's as simple to use as wiring up a couple of events.

This should get you started.

Alternatively you can use Dispatcher.Invoke to perform the operation on the correct thread:

private void DoStuffOnThread()
{
    Dispatcher.Invoke(new Action(DoStuffOnUIThread));
}

private void DoStuffOnUIThread()
{
    // ...
}
Share:
14,014
Admin
Author by

Admin

Updated on June 09, 2022

Comments

  • Admin
    Admin almost 2 years

    I have a WPF window in the main thread. On button clock of this window i am loading the data. Meanwhile i am using a seperate thread to display a wait screen. But i am not able to set the main window as the parent of the wait screen. It throws following error: The calling thread cannot access this object because a different thread owns it