|
Dear All, Good day.
Need your help on threading.
I want to use a wpf window as progress window across my project.
On the main UI thread while doing the business logics i need to open a new wpf window in separate thread as progress window and once the Business logic is done i want to close the same.
I googled and found couple of examples to open a wpf window in separate thread and with new STA.. but am not able to find how to close the same window once done.
My progress window doesn't allow the user to close it.
global variable
progresswindow _pw = new progresswindow();
ShowProgress()
{
_pw.Show();
}
HideProgress()
{
_pw.Hide();
}
MainThreadBusinessLogic()
{
LogicStarts...
ShowProgress();
LogicEnds..
HideProgress();
}
This is wat i wanted to do. But as the main thread is very busy it wont show the progress window i know. I used the background worker and showed the progress bar.. But the thing is i have different business logic which will work based on the users clicks.. so i may need to create different workers for handling those.
If i do something like above mentioned window as progress window i can commonly use the same for all places.
Hence request your help here my friends.
thanks in advance.
|
|
|
|
|
You are doing it all backwards.
All UI lives on the main thread (including the progress window)
All business logic lives on a background thread
When you need to update the progress dialog from the background thread, use Dispatcher.Invoke.
|
|
|
|
|
Hi Sledge,
Thanks for your reply.
And you are right i am doing in the back ward only.
But the thing is, I have more the 20 forms in my application which have different functionality. And even in single form i have more than 5 buttons which will do different business logic's. So that is reason i wanted to do this approach, other wise i need to create separate background workers for all buttons or single background worker with different parameters.. which is more messy.
So i planned to have a separate progress window which can be accessed across all the forms.
But as we know both are in same thread causing issue. i planned to open and use the new window in different thread.
Expecting your reply thanks in advance.
|
|
|
|
|
Hema Bairavan wrote: which is more messy
LOL... no, it's not. The way you have it now won't even work. The main UI will be completely unresponsive, have painting issues, etc.
You can use async functions, lamba expressions, etc.
Bottom line is, you'll have to move your business logic to the background thread in order to make your app work.
|
|
|
|
|
Try looking at the BackgroundWorker class. It provides an encapsulation for a worker thread that supports passing updates to the UI taking care of all the dirty cross-thread marshaling. It is very easy to use and supports aborting as well.
As for your progress window, you need to raise it when the first background thread is started and close it when the last background thread ends. You may want to consider creating the form at app startup then setting all of the thread progress notifications to call a notify update method on the form. The form could then determine if it needs to show/hide and which thread(s) progress to show. Again, if you use a backgroundworker, there is a thread complete event which fires when the thread is done making it easy to do something "on the way out the door". The progress window should be event-driven... only responding to events raised by the worker threads.
|
|
|
|