locked
backgroundworker component and progressbar

    Question

  • hey guys i was wondering if you could help me,

     

    I have a for that runs a data bindingsource filter on a large table of dataand would like to implement a progress bar...

     

    I have declared the backgroundworker component and put the cpu instensive code in there and also set up a progressbar component as you can see below...

     

    private void simpleButton1_Click(object sender, EventArgs e)

    {

    backgroundWorker1.RunWorkerAsync();

    }

     

     

     

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)

    {

    exposureViewBindingSource.Filter = "Exposure_ID <> 0";

    bool today = false;

    if (dateEdit1.DateTime == DateTime.Today && dateEdit2.DateTime == DateTime.Today)

    {

    exposureViewBindingSource.Filter += " AND Date =' " + DateTime.Today + " '";

    today = true;

    }

    if (dateEdit1.EditValue != null && dateEdit2.EditValue != null && today == false)

    {

    exposureViewBindingSource.Filter += " AND Date >=' " + dateEdit1.DateTime + " ' AND Date <=' " + dateEdit2.DateTime + " '";

    }

    if (lookUpEdit2.EditValue != null)

    {

    exposureViewBindingSource.Filter += " AND Programme_ID =' " + Convert.ToInt32(lookUpEdit2.EditValue) + " '";

    }

    if (lookUpEdit3.EditValue != null)

    {

    exposureViewBindingSource.Filter += " AND Brand_ID =' " + Convert.ToInt32(lookUpEdit3.EditValue) + " '";

    }

    }

     

     

     

    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)

    {

    progressBar1.Value = e.ProgressPercentage;

    }

     

     

     

    For some reason, the progressbar is not moving when the button that launches the thread is clicked.

     

    Is there any particular reason for this as I don't see any difference in my code to a lot of the examples out there on the net.

     

    Thanks for the help guys...

     

    John

    Friday, May 11, 2007 3:38 PM

Answers

  • Hi, John

    The backgroundWorker.ProgressChanged when its ReportProgress method is invoked.

    However, you didn't call the method at all according to the code snippet.

    Thanks

    Tuesday, May 15, 2007 10:04 AM
  • Additionally, here is an example of using progressBar with backgroundWorker:

    Code Snippet

            private void button1_Click(object sender, EventArgs e)

            {

                progressBar1.Maximum = 100;

                backgroundWorker1.WorkerReportsProgress = true;

                backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);

                backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);

                backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);

                backgroundWorker1.RunWorkerAsync();

            }

     

            private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)

            {

                for (int i = 0; i < 100; i++)

                {

                    Thread.Sleep(100);

                    backgroundWorker1.ReportProgress(i); //run in back thread

                }

            }

     

            private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) //call back method

            {

                progressBar1.Value = e.ProgressPercentage;

            }

            private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) //call back method

            {

                progressBar1.Value = progressBar1.Maximum;

            }

     

     

    Hope it helps.

    Thanks

    Tuesday, May 15, 2007 10:14 AM

All replies

  • Hi, John

    The backgroundWorker.ProgressChanged when its ReportProgress method is invoked.

    However, you didn't call the method at all according to the code snippet.

    Thanks

    Tuesday, May 15, 2007 10:04 AM
  • Additionally, here is an example of using progressBar with backgroundWorker:

    Code Snippet

            private void button1_Click(object sender, EventArgs e)

            {

                progressBar1.Maximum = 100;

                backgroundWorker1.WorkerReportsProgress = true;

                backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);

                backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);

                backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);

                backgroundWorker1.RunWorkerAsync();

            }

     

            private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)

            {

                for (int i = 0; i < 100; i++)

                {

                    Thread.Sleep(100);

                    backgroundWorker1.ReportProgress(i); //run in back thread

                }

            }

     

            private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) //call back method

            {

                progressBar1.Value = e.ProgressPercentage;

            }

            private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) //call back method

            {

                progressBar1.Value = progressBar1.Maximum;

            }

     

     

    Hope it helps.

    Thanks

    Tuesday, May 15, 2007 10:14 AM
  • I have a problem with this snippet.
    When the progressBar reach the end, it restart again for the second time.
    Why this strange behaviour?

    Luigi
    Thursday, December 17, 2009 8:17 PM