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.
LVL 1
JElsterAsked:
Who is Participating?
 
dsaboConnect With a Mentor Commented:
 You need to call the close method of the form on the main thread. To do this, you could use an event, that is raised when the thread finish.

     Create an event an rease it when the long code finish running.

      private event EventHandler ThreadFinished;
------------------
Some were before the thread starts

       this.ThreadFinished += new EventHandler(Form1_ThreadFinished);
------------------

     private void Form1_ThreadFinished(object sender, EventArgs e)
    {
         this.Close();
    }
---------------
at the end of the long process, raise the event

     ThreadFinished (this,new EventArgs())

Hope it helps, DS
0
 
ryerrasCommented:
0
 
JElsterAuthor Commented:
I need it for WinForms app.. thanks
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
dsaboCommented:
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
0
 
Fahad MukhtarDistinguished EngineerCommented:
within the loop body you can do it without thread
just put
Application.DoEvents() in the loop
0
 
JElsterAuthor Commented:
How can use the existing winforms progress bar... I tried a timer but it does seem to show any progress.. thanks
0
 
dsaboCommented:
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
     

       
0
 
dsaboCommented:
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
0
 
JElsterAuthor Commented:
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();
            }
0
 
dsaboCommented:
Are you starting the timer???

timerProgess.Start();
0
 
JElsterAuthor Commented:
Yes... do I need run it in another thread.. the UI does not update? thanks
0
 
dsaboCommented:
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.
0
 
JElsterAuthor Commented:
Still doesn't work... any other ideas? Thanks... I step through it and timer never fires even when enabled=true ???
0
 
dsaboCommented:
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.
0
 
JElsterAuthor Commented:
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?
0
 
JElsterAuthor Commented:
Thanks!!!!!!!!!!!!!!!!!!!!
0
All Courses

From novice to tech pro — start learning today.