Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update the progress bar in runtime using c#

Tags:

c#

I am using the below code to update my progress bar.

        ProgressBar.Visible = true; 
        ProgressBar.Minimum = 1; 
        ProgressBar.Maximum = PortCount; 
        ProgressBar.Value = 1; 
        ProgressBar.Step = 1; 

        int intdata = 5;
        for (int x = 1; x <= intdata; x++)
          {
            ProgressBar.PerformStep();
        }

        MessageBox.Show("Done");

But, it is not getting updated during runtime. Is it because the progress bar is in the same thread. If so, how to update this progress from another thread. Help...

like image 481
Anuya Avatar asked Apr 14 '10 06:04

Anuya


1 Answers

You are not giving the message pump time to update the control.

Although either of these are bad, you can do:

  • Call Refresh on the control
  • Call Application.DoEvents
like image 118
leppie Avatar answered Nov 04 '22 05:11

leppie