Link to home
Start Free TrialLog in
Avatar of JElster
JElsterFlag for United States of America

asked on

Update a Progress Bar using a new thread

I have a form with a progress bar and button.. the button executes a long process.. how can I have the progress bar 'run' in a another process while the long process is going? Just need to show the progress bar advancing.
Avatar of ryerras
ryerras

Avatar of JElster

ASKER

I need it for WinForms app.. thanks
Hello,

How you will count the progress of the process? By time, or the process have a series of steps that you could handle?

I will get back to you as soon you answer the above key question.

Regards, DS
within the loop body you can do it without thread
just put
Application.DoEvents() in the loop
Avatar of JElster

ASKER

How can use the existing winforms progress bar... I tried a timer but it does seem to show any progress.. thanks
Alright.

   First, you need in the progress bar the following properties:
   
         progressBar1.Minimum= 0;  //Minimum value that the progress bar will have. At this value, the progress is null.
         progressBar1.Maximum= 100;  //Maximum value that the progress bar will have. At this value, the progress is full or completed.    

   Then if you use the timer you need to set every time that the timer ticks, this propery.

         progressBar1.Value += 1; //or any other number.

   The Value propertie indicates the current status of the bar. Example.


         progressBar1.Minimum= 0;
         progressBar1.Maximum= 100
         progressBar1.Value = 50;

         In this example, the progressBar will be in the middle.

    Other way to do this is to set the propertie Step. That set the amount of progress that every step will increment the Value Property.
         progressBar1.Step = 1;

     To perform a step, just call the method PerformStep()
         progressBar1.PerformStep();

     Example using the above method:

         progressBar1.Minimum= 0;
         progressBar1.Maximum= 100;
         progressBar1.Step = 1

   Then if you use the timer you need to set every time that the timer ticks, this propery.

         progressBar1.Value += 1; //or any other number.


Regards, DS
     

       
Alright.
CORRECT ANSWER... I hit by mistake the submit button.
********************************************

   First, you need in the progress bar the following properties:
   
         progressBar1.Minimum= 0;  //Minimum value that the progress bar will have. At this value, the progress is null.
         progressBar1.Maximum= 100;  //Maximum value that the progress bar will have. At this value, the progress is full or completed.    

   Then if you use the timer you need to set every time that the timer ticks, this propery.

         progressBar1.Value += 1; //or any other number.

   The Value propertie indicates the current status of the bar. Example.


         progressBar1.Minimum= 0;
         progressBar1.Maximum= 100
         progressBar1.Value = 50;

         In this example, the progressBar will be in the middle.

    Other way to do this is to set the propertie Step. That set the amount of progress that every step will increment the Value Property.
         progressBar1.Step = 1;

     To perform a step, just call the method PerformStep()
         progressBar1.PerformStep();

     Example using the above method:

         progressBar1.Minimum= 0;
         progressBar1.Maximum= 100;
         progressBar1.Step = 1
         progressBar1.Value = 1; //Set the initial value.

   Then if you use the timer you need to set every time that the timer ticks, this propery.

         progressBar1.PerformStep();

Regards, DS
Avatar of JElster

ASKER

No luck... Here's my code.. thanks


                               // Long process
            private void btnOK_Click(object sender, System.EventArgs e)
            {
                  // Handle Addition RepIds
                  Cursor tmp = this.Cursor;
                  this.Cursor = Cursors.WaitCursor;

                  
                  this.timerProgress.Enabled = true;
                  this.progressBar.Visible = true;
                                             
                                                 ......

                               }

      private void UpdateProgress()
            {
                  if (ProgressInt >= 25)
                  {
                        this.ProgressInt = 1;
                  }
                  else
                  {
                        this.ProgressInt++;
                  }
                  this.progressBar.Value = this.ProgressInt;
            
            }

            private void timerProgress_Tick(object sender, System.EventArgs e)
            {
                  this.UpdateProgress();
            }
Are you starting the timer???

timerProgess.Start();
Avatar of JElster

ASKER

Yes... do I need run it in another thread.. the UI does not update? thanks
No, actually the timer runs in another thread by default.

  private void btnOK_Click(object sender, System.EventArgs e)
          {
               // Handle Addition RepIds
               Cursor tmp = this.Cursor;
               this.Cursor = Cursors.WaitCursor;

               
               this.timerProgress.Enabled = true;
               this.progressBar.Visible = true;
               this.progressBar.Minmum = 0;
               this.progressBar.Maximum = 25;  //or any other number
               this.progressBar.Value = 0;
               this.progressBar.Step = 1;
             
                                             
                                                 ......

              }

 private void UpdateProgress()
          {
               if (this.progressBar.Value == 25)
               {
                     this.progressBar.Value = 1;
               }
               else
               {
                   this.progressBar.PerformStep();
               }        
          }

try this out, and let me know.
Avatar of JElster

ASKER

Still doesn't work... any other ideas? Thanks... I step through it and timer never fires even when enabled=true ???
You should change the approach.

Try running the long process in another thread, instead of the main thread, and see what happens. If you need help on how to do it, let me know.
Avatar of JElster

ASKER

That worked!  But...  how do I  Join the threads... at the end of my long process I  'Close' at the end of the long process..but the form does not close?
ASKER CERTIFIED SOLUTION
Avatar of dsabo
dsabo
Flag of Venezuela, Bolivarian Republic of image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JElster

ASKER

Thanks!!!!!!!!!!!!!!!!!!!!