Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Winforms/WPF program is an <code>Eventing</code> system. There is a single thread which continuously processes events from an event queue. That is its main job and ideally that is the only thing which it should do. Any sort of UI activity generates events in the event queue - like you move your mouse over the window or click something or some other window overlaps your window and then again when it goes away from the overlapped position. All these events are processed by the UI thread and that keeps the UI <em>updated</em> all the time.</p> <p>Further, Winforms/WPF make it necessary to access and/or update controls and their properties in a thread safe manner by allowing it only on the UI thread. </p> <p>If you block this UI thread or do some other CPU bound calculation on it, then your UI <em>responsiveness</em> and <em>updated</em> behavior will suffer. Worst case UI will freeze.</p> <p>Hence the correct answer for you is to do your calculation loop on another worker thread and only update the progress bar UI by marshaling the call to UI thread using the <code>Dispatcher</code>.</p> <p>However, to answer your question and satisfy your inquisition, here is something that is possible - but it is <strong>bad practice</strong> and your should <strong>never do the following...</strong>:</p> <p>To make it simple, when you update the <code>Value</code> property of the progress bar, it invalidates the progress bar UI - so, UI must update. Hence lets say an event is generated in the event queue which will cause some code to run which will update the UI. However, you are running in a loop over the UI thread - so, the thread has no chance to process this event unless your loop is over. Hence you don't see any UI update. The trick is to make the UI thread process that event before you make the next update on the <code>Value</code> of progress bar. You can do this by forcefully invoking a lower priority item into the event queue - so that normal and higher priority items are processed before going to the next iteration.</p> <pre><code>using (var pu = new ProgressBarUpdate(pb, totalCount)) { for (int x = 0; x &lt; totalCount ; x++) { // operations here pu.UpdateProgress(); Dispatcher.Invoke(DispatcherPriority.Background, new Action(()=&gt;{})); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload