Rich K

Rich K

  • NA
  • 3
  • 2.5k

Progress Bar in C# Winforms App

Apr 3 2014 10:06 AM
I have no trouble getting a progress bar to work in a sample c# winforms app when using a BackgroundWorker and doing something like this:

private void Form1_Load(object sender, EventArgs e)
        {
            // Start the BackgroundWorker
            // Creates thread and DoWork method begins.
            backgroundWorker1.RunWorkerAsync();
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            for (int i = 1; i <= 100; i++)
            { 
                // Wait 100 ms at each iteration in order to see the progress
                Thread.Sleep(100);

                // Report Progresss
                backgroundWorker1.WorkerReportsProgress = true;
                backgroundWorker1.ReportProgress(i);
            }
        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            // Change the value of the ProgressBar to the BackgroundWorker progress.
            progressBar1.Value = e.ProgressPercentage;

            // Set the text of the titlebar
            this.Text = e.ProgressPercentage.ToString();
        }



but this just adds to the progress bar each time through the loop until reaching 100. In a real application though I want to start the progress bar when I select a file, then add progress to it until the entire process of retrieving data data from the file selected, build a sql query, executing the query and writing to a file completes. Since I'm not just adding to the bar each time through a loop I'm not sure how to go about doing this. Could anyone provide a real example? 


Thanks


Answers (2)