WPF - synchronous UI update
If I understand correctly changing any control in WPF (eg. text of a Label) enqueues an update in Dispatcher. Dispatcher waits for my code to finish and when it has time it processes the whole queue.
For this, calling
double current = 0;
ReportProgress (0d, "ProcessingStarted");
foreach (var item in collection)
{
item.Process(); //takes about 10s
current++;
ReportProgress (current / (double)collection.Count, "Processing item No. " + current.ToString () + " finished");
}
ReportProgress (1d, "Finished");
where ReportProgress invokes an event with this event handler
private void handlerProgressMade (object sender, ProgressEventArgs e)
{
pbProgress.Value = pbProgress.Maximum * e.Percent;
lbProgressMessage.Content = e.Message;
}
ends up with full progressbar and message "Finished" displayed, but the intersteps are not shown.
I am aware that it could be done by calling the function in different thread and updating UI asynchronously (and it is going to happen at some point), but for now adding threads seems like unnecessary complication.
How to开发者_运维百科 force the Dispatcher to update and redraw immediately?
if you dont want to use background thread, then before and after you set the progress bar value, old hack of Application.DoEvents()
might help as it will wait to clear all message queue that got piled up in the GUI Message Bus
精彩评论