wpf c# status window update
I am trying to create a status window which content(textbox) should change in a lengthy operation. This status window is called and up开发者_开发技巧dated from the main application. Unfortunately, the content is updated only at the finish of the operation. I am using VS2008, C# and WPF.
Thank you.
This is in another answer to allow better formatting:
The code should like pretty much like this:
BackgroundWorker bgWorker = new BackgroundWorker();
bgWorker.DoWork += new DoWorkEventHandler(bgWorker_DoWork);
bgWorker.RunWorkerCompleted += new
RunWorkerCompletedEventHandler(bgWorker_RunWorkerCompleted);
bgWorker.RunWorkerAsync();
In the bgWorker_DoWork
method, call "Dispatcher.Invoke
" (or "Dispatcher.BeginInvoke
", depends on the circumstances) and the delegate for the invoke may update your textbox (because it's in the same thread as the textbox).
If you are calling a service and the call is synchronous, the main application does not get any chance to update. You need to put them on a different thread. Also, if the service call is getting a higher priority (UI threads are always of less priority), you need to forcibly tell the system to update these.
How about doing the lengthy operation in a BackgroundWorker thread and notifying the content every once in a while by using the Dispatcher? It will give a responsive yet progressive feeling to your UI.
精彩评论