Note that there are some explanatory texts on larger screens.

plurals
  1. PODisplay progress bar while doing some work in C#?
    text
    copied!<p>I want to display a progress bar while doing some work, but that would hang the UI and the progress bar won't update.</p> <p>I have a WinForm ProgressForm with a <code>ProgressBar</code> that will continue indefinitely in a <strong>marquee</strong> fashion.</p> <pre><code>using(ProgressForm p = new ProgressForm(this)) { //Do Some Work } </code></pre> <p>Now there are many ways to solve the issue, like using <code>BeginInvoke</code>, wait for the task to complete and call <code>EndInvoke</code>. Or using the <code>BackgroundWorker</code> or <code>Threads</code>. </p> <p>I am having some issues with the EndInvoke, though that's not the question. The question is which is the best and the simplest way you use to handle such situations, where you have to show the user that the program is working and not unresponsive, and how do you handle that with simplest code possible that is efficient and won't leak, and can update the GUI.</p> <p>Like <code>BackgroundWorker</code> needs to have multiple functions, declare member variables, etc. Also you need to then hold a reference to the ProgressBar Form and dispose of it.</p> <p><strong>Edit</strong>: <code>BackgroundWorker</code> is not the answer because it may be that I don't get the progress notification, which means there would be no call to <code>ProgressChanged</code> as the <code>DoWork</code> is a single call to an external function, but I need to keep call the <code>Application.DoEvents();</code> for the progress bar to keep rotating.</p> <p>The bounty is for the best code solution for this problem. I just need to call <code>Application.DoEvents()</code> so that the Marque progress bar will work, while the worker function works in the Main thread, and it doesn't return any progress notification. I never needed .NET magic code to report progress automatically, I just needed a better solution than :</p> <pre><code>Action&lt;String, String&gt; exec = DoSomethingLongAndNotReturnAnyNotification; IAsyncResult result = exec.BeginInvoke(path, parameters, null, null); while (!result.IsCompleted) { Application.DoEvents(); } exec.EndInvoke(result); </code></pre> <p>that keeps the progress bar alive (means not freezing but refreshes the marque)</p>
 

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