简体   繁体   中英

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 updated 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM