[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1445
  • Last Modified:

Winforms - How to update a progress bar in another thread

Hi..
I have a Save function that takes a long time. I would like to have a progress bar show something during the save. I created a user control with a progress bar and timer.. that just loops through and increments the bar and resets it.
When I run my code nothing happens... how do I show/refresh the progress bar in my Save() method? (WinForms)
thanks!


 private void btnSave_Click(object sender, EventArgs e)
  {
     Save():

}

private void Save()
{
    // Show progress bar
    progressBar.Start();


  /// Long code to save and update multiple tables...


   // Stop and hide the bar
   progressBar.Stop():



}
0
JElster
Asked:
JElster
  • 3
  • 2
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
From the description of your "progress bar", it doesn't actually need any feedback from the saving code since it just loops to show progress is being made.

In light of this, just put your "/// Long code to save and update multiple tables..." on a seperate thread.

Something like...

        private delegate void SaveComplete();

        private void btnSave_Click(object sender, EventArgs e)
        {
            btnSave.Enabled = false;
            Save();
        }

        private void Save()
        {
            // Show progress bar
            progressBar.Start();  // runs in the UI thread (looping your progress bar)
           
            System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(Worker));
            t.Start();
        }

        private void Worker()
        {
            // this is running in its own thread...

            /// Long code to save and update multiple tables...

            // simulated save...
            System.Threading.Thread.Sleep(5000);

            WorkComplete();
        }

        private void WorkComplete()
        {
            // marshal the threaded call back onto the main UI thread
            if (this.InvokeRequired)
            {
                SaveComplete sc = new SaveComplete(WorkComplete);
                this.Invoke(sc);
            }
            else
            {
                progressBar.Stop();
                btnSave.Enabled = true;
            }
        }
0
 
JElsterAuthor Commented:
Hi..
My save method (Worker()) displays a MessageBox.., it a large transaction....may need to rollback... will this screw thing up?  Updating the UI on a different thread...?
thanks again
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Right...you shouldn't display a message box from the worker thread.

Can you ask the question before starting the worker?

Or does the worker calculate some things and then ask the user for confirmation?
0
 
JElsterAuthor Commented:
It asks for confirmation... half way through several tables... any ideas? thanks
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You would have to break the Woker() sub into multiple parts.

Notice how when we call WorkComplete(), that the "else" block is the part that can work with the UI?  So you would do the first part in your thread and call WorkComplete().  Then in the "else" block ask your question.  If confirmed, you would start another thread just as we did in the Save() method but using a different method.  Repeat the process for as many "sections" as you have requiring User interaction.

Make sense?

If you have variables that would need to keep their state between confirmations then declare them at the form level so that the threads can access them.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now