In my main window (Thread A), I start a new thread (Thread B) which does some work while the user waits.
Thread B fires events if there is an error or extra information is required from the user, Thread A will listen to these events.
In Thread A’s event listener I need to show dialog messages to the user, I have a custom dialog window and show it using dialogWindow.showDialog(). This works fine, but causes an error when I try and set the owner of the dialog, I do this dialogWindow.Owner = Window.GetWindow(this).
The error that I get is: The calling thread cannot access this object because a different thread owns it.
What is the correct way to listen for events that are fired from a different thread?
The correct way to raise an event to the UI thread from the background thread is that, the event should be raised on that Dispatcher,
Key here is get the dispatcher of UIthread before hand.
when the UI thread listens to the raised event it can set the Owner property(as the window was created by the UI thread).