Question

I'm setting up a background worker for the first time. It is mostly working as the code runs and my stop/cancel button is working. However, I am also trying to report progress to update a progress bar but I cannot get this to fire at all.

I start the code from a button click which runs this code:

backgroundWorker1.WorkerSupportsCancellation = true;
backgroundWorker1.WorkerReportsProgress = true;
backgroundWorker1.RunWorkerAsync();//this invokes the DoWork event 

My Do_Work method:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;

            int j = 0;// Count cumulative imported files
            int countDupFiles = 0;// Count number of previously imported csv files
            int countImportedFiles = 0;// Count imported files


            foreach (string folderPath in csvDirList)
            {
                string[] csvFileNames = Directory.GetFiles(@folderPath, "*.csv");
                frmImportCsvData.replaceAll(csvFileNames, folderPath + "\\", "");

                for (int i = 0; i < csvFileNames.Length; i++, j++)
                {
                    string csvFilePath = folderPath + "\\" + csvFileNames[i];

                    if ((worker.CancellationPending == true))
                    {
                        e.Cancel = true;
                        break;
                    }
                    else
                    {
                        if (dataLayer.ImportCsvDataBkgrnd(this, csvFilePath, compIdValue, csvFileCount, i))//new method processes subdirectories if tick box selected
                        {
                            countImportedFiles = countImportedFiles + 1;
                        }
                        else
                        {
                            countDupFiles = countDupFiles + 1;
                        }

                        System.Threading.Thread.Sleep(500);

                    }

                    worker.ReportProgress(j);//tried using worker and backgroundWorker1 but neither works
                    backgroundWorker1.ReportProgress(j);

                    //string proj = j.ToString();
                    //MessageBox.Show(proj);//Displays incrementing j as expected when not commented out
                }
            }
            if (countImportedFiles > 0)
                MessageBox.Show(countImportedFiles + " files were imported.");
            if (countDupFiles > 0)
                MessageBox.Show(countDupFiles + " files were not imported. Matches all ready in Database.");
        }

Trying to fire either of these ProgressChanged events:

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    string tbProgress = (e.ProgressPercentage.ToString() + "%");
    MessageBox.Show(tbProgress + "backgroundWorker1");
    importProgressBar(e.ProgressPercentage);
}

private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    string tbProgress = (e.ProgressPercentage.ToString() + "%");
    MessageBox.Show(tbProgress + "worker");
    importProgressBar(e.ProgressPercentage);
}

Finally, I want the ProgressChanged event to trigger this method to update my progress bar:

public void importProgressBar(int i)
{
    progressTableLayoutPanel.Visible = true;//display progress bar

    int percProgress = 100 * (i + 1) / csvFileCount;

    if (percProgress <= 99)// Required to prevent values above 100 that crash the code
        progressBar.Value = percProgress + 1;//hack that makes the progress bar update when progress value decreases
    progressBar.Value = percProgress;
    percProgressLabel.Text = percProgress.ToString();

    progressTableLayoutPanel.Update();//Required to display all progress bar table contents
    //Thread.Sleep(200);

    if (percProgress >= 100)
    {
        Thread.Sleep(200);
        progressTableLayoutPanel.Visible = false;
    }
}

The cancel button code, which works, looks like this:

private void stopImportButton_Click(object sender, EventArgs e)
        {
             backgroundWorker1.CancelAsync();
        }

The messageboxes in my ProgressChanged events never show up and my progress bar is never set to visible. Any ideas what the problem could be?

Was it helpful?

Solution

Check this example:

    BackgroundWorker bgw = new BackgroundWorker();       
    public Form1()
    {
        InitializeComponent();
        label1.Text = "";
        label2.Text = "";
    }

   private void button1_Click_1(object sender, EventArgs e)
{
    if (bgw == null)
    {
        bgw = new BackgroundWorker();
        bgw.DoWork += new DoWorkEventHandler(bgw_DoWork);
        bgw.ProgressChanged += new ProgressChangedEventHandler(bgw_ProgressChanged);
        bgw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgw_RunWorkerCompleted);
    }
    bgw.WorkerReportsProgress = true;
    bgw.WorkerSupportsCancellation = true;
    bgw.RunWorkerAsync();
}

    void bgw_DoWork(object sender, DoWorkEventArgs e)
    {
        int total = 57; //some number (this is your variable to change)!!

        for (int i = 0; i <= total; i++) //some number (total)
        {
            System.Threading.Thread.Sleep(100);
            int percents = (i * 100) / total;
            bgw.ReportProgress(percents, i);
            //2 arguments:
            //1. procenteges (from 0 t0 100) - i do a calcumation 
            //2. some current value!
        }
    }

    void bgw_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        progressBar1.Value = e.ProgressPercentage;
        label1.Text = String.Format("Progress: {0} %", e.ProgressPercentage);
        label2.Text = String.Format("Total items transfered: {0}", e.UserState);
    }

    void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
         //do the code when bgv completes its work
    }
}

Maybe this helps you with your problem...

And try to put the progress to visible just after you call the background.doWork in the button click event.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top