Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As Marc said, you want to make sure that you spin off a new thread to do your long running computation. That way the User Interface thread (which is the one that has to do all the screen updates) can redraw the progres bar whenever you change the percent complete. </p> <p>It's important to note that <strong>only</strong> the UI thread can update the interface. So, once you are running on a separate thread, you have to go through an extra hoop to make sure that your UI change is processed on the UI thread. If you aren't sure what thread you are running on, you can check the value of InvokeRequired (if your class is a System.Windows.Form) to see if you are actualy in the UI thread. </p> <p>To get your command processed on the UI thread, use the Control.Invoke() function to make sure the update is processed on the UI thread for the control you are working with.</p> <p>In my sample code below I'm creating a delegate function type and declaring the invoked function in advance....I've not done it with any of the cool C# 3.5 functions, but I bet you could work up a lamba expression to do the same thing. </p> <pre><code> private void bCreateInvoices_Click(object sender, EventArgs e) { BackgroundWorker worker = new BackgroundWorker(); worker.DoWork += new DoWorkEventHandler(CreateInvoices); worker.RunWorkerAsync(this); } // Here is the long running function that needs to update the progress bar public void CreateInvoices(object sernder, DoWorkEventArgs e) { int totalChecked = CountCheckedServiceOrders(); int totalCompleted = 0; foreach (...data to process...) { totalCompleted++; if (InvokeRequired) { Invoke(new Change(OnChange), "status text", totalCompleted, totalChecked); } } } // this code updates the status while a background thread works private delegate void Change(string status, int complete, int total); private void OnChange(string status, int complete, int total) { if (status == null) { progressBar.Visible = false; lStatus.Text = "Task complete"; progressBar.Value = 0; } else { progressBar.Visible = true; progressBar.Minimum = 0; progressBar.Maximum = total; progressBar.Value = complete; lStatus.Text = status; } } </code></pre> <p>Take a look at the <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.control.invokerequired.aspx" rel="nofollow noreferrer">MSDN Control.InvokeRequired manual page</a> and the <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.control.invoke.aspx" rel="nofollow noreferrer">MSDN Control.Invoke manual page</a> for some more info.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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