Click here to Skip to main content
15,903,203 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi guyz,

i have write a code to run two progress bar at same time, but when i run the program the progressBar1 starts and ends by displaying the messageBox"Thread Complete", and then the second progressBar2 starts. i just wanted to know that the both progressBar1 and progressBar2 starts on the same time..


C#
private void btnStart_Click(object sender, EventArgs e)
       {
           if (isProcessRunning)
           {
               MessageBox.Show("A process is already running.");
               return;
           }
           Thread backgroundThread = new Thread(
               new ThreadStart(() =>
               {
                   isProcessRunning = true;
                     //progressBar1 Starts here
                   for (int n = 0; n < 100; n++)
                   {
                       Thread.Sleep(50);
                       progressBar1.BeginInvoke(new Action(() => progressBar1.Value = n));
                   }
                   MessageBox.Show("Thread completed!");
                   if (progressBar1.InvokeRequired)
                       progressBar1.BeginInvoke(new Action(() => progressBar1.Value = 0));
                     //progressBar1 Starts here
                   for (int m = 0; m < 100; m++)
                   {
                       Thread.Sleep(50);
                       progressBar2.BeginInvoke(new Action(() => progressBar2.Value = m));
                   }
                   MessageBox.Show("Thread completed!");
                   if (progressBar2.InvokeRequired)
                       progressBar2.BeginInvoke(new Action(() => progressBar2.Value = 0));
                   isProcessRunning = false;
               }
           ));
           backgroundThread.Start();
       }
Posted
Updated 26-Nov-13 23:26pm
v3

 
Share this answer
 
v2
You could also add the line
C#
progressBar2.BeginInvoke(new Action(() => progressBar2.Value = n));

to the first loop to fill both progress bars at the same time.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900