views:

114

answers:

3

Hello

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.

+1  A: 

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.

Kangkan
+1  A: 

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.

Could you please give me an example on how to do this?
phm
+3  A: 

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

BackgroundWorker already supports progress updating (via BackgroundWorker.ProgressChanged). Why would you want to add another level of indirection with Dispatcher Invoke/BeginInvoke?
micahtan