I don’t think I’ve quite found what I’m looking for, and unsure if possible… I’ve worked with background workers to do processes and during certain “ProgressChanged” events to pull messages from the background worker and update a given window dialog. No problem.
New item… During a certain process that I am trying to have running on a background worker, depending on certain condition, I need it to actually call up a new Window dialog to get user input before resuming its operation… In pseudo format..
Start Window
Click Button For Action
Create BackgroundWorker (BGW) and enable progress changed
Link BGW "ProgressChanged" to CurrentWindowCheckProgress method
run BGW process
In the BGW, under certain condition, I update the progress and have a flag on the BGW that I need specific dialog X to be shown to the user. So, via the ReportProgress(), the hook to the CurrentWindowCheckProgress method is hit (which is on the UI thread). This method sees the flag and needs to create a new Window(), get answer from that, THEN return back to the BGW thread to continue.
So far, all the logic works as planned, but I’m getting an error
“The calling thread must be STA, because many UI components require this.”
So, how / what do I need to do to show the user my interim form “TestPromptWindow”.
This is C#, WPF, VS2010, .Net 4.0
This is not what backgroundworkers are designed for. The messaging BGW does for you is completely asynchronous. Technically it is posssible to use Control.Invoke to run code on the UI thread, but it could be that this Invoke call is run out of order with your progress updates, as it may have a different message priority.
The next release of .net will provide very nice support for this. meanwhile, you can use the
Taskclass, especcially itscontinueWithmember. Here is a nice example showing how to go back and forth to the UI thread.