Reputation: 13
In the MainWindow, I could call MyMethod from DoWork event easily, and that works fine since MyMethod doesn't have access to any UI control.
My question is how to call the same method (MyMethod) in the child window without instantiation a new object, where the following statement works correctly outside the BackgroundWorker
((MainWindow)this.Owner).MyMethod();
But inside the BackgroundWorker (in the child Window) this statement throws an Exception, although it doesn't have any access to UI and it could be called inside the BackgroundWoerker in the MainWindow.
Any attempt to help me will be appreciated.
Upvotes: 0
Views: 1344
Reputation: 1575
When calling RunWorkerAsync(), consider using the overload that takes an object. If you are calling RunWorkerAsync() from the UI thread of the child window, you could instead call
bw.RunWorkerAsync(this.Owner);
You can then pull that object out of the DoWork event args and invoke your method on that object.
((MainWindow)e.Argument).MyMethod();
Upvotes: 0
Reputation: 3255
You need to invoke the call on the Dispatcher UI thread:
Application.Current.Dispatcher.Invoke(() => { MyMethod() });
Upvotes: 0