ebolton
ebolton

Reputation: 1146

BackgroundWorker for loops/How they work

I'm a bit new to using backgroundWorker and been doing some research online on how to use it. From the examples I'm getting there is something that doesn't make sense. In your DoWork function you run a loop from 0-100 or 1-10 and that loop basically tells your progress bar what your progress is, and inside that loop you do all your hard work. Well what if you have some job to do that is a loop, for example looping through a list and printing the values to a file?

Here's code that I've been playing with.

        void m_oWorkder_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        if (e.Cancelled)
        {
            taskLabel.Text = "Task Cancelled";
        }

        else if (e.Error != null)
        {
            taskLabel.Text = "Error while performing background operation.";
        }
        else
        {
            taskLabel.Text = "Task Completed...";
        }

        startButton.Enabled = true;
        cancelButton.Enabled = false;


    }

    void m_oWorkder_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        progressBar1.Value = e.ProgressPercentage;
        taskLabel.Text = "Processing ... " + progressBar1.Value.ToString() + "%";
    }

    void m_oWorkder_DoWork(object sender, DoWorkEventArgs e)
    {
        for (int i = 0; i < 100; i++)
        {
            someRandom();
            m_oWorker.ReportProgress(i);
            if (m_oWorker.CancellationPending)
            {
                e.Cancel = true;
                m_oWorker.ReportProgress(0);
                return;
            }
        }
        m_oWorker.ReportProgress(100);


    }

    private void startButton_Click(object sender, EventArgs e)
    {
        startButton.Enabled = false;
        cancelButton.Enabled = true;

        m_oWorker.RunWorkerAsync();
    }

    private void cancelButton_Click(object sender, EventArgs e)
    {
        if (m_oWorker.IsBusy)
        {
            m_oWorker.CancelAsync();
        }
    }

    private void someRandom()
    {


        int x = 0;
        while (x != 100)
        {
            x++;
            Console.WriteLine("Value of x = " + x);

        }

    }

Upvotes: 0

Views: 1721

Answers (2)

Blorgbeard
Blorgbeard

Reputation: 103467

In your DoWork function you run a loop from 0-100 or 1-10 and that loop basically tells your progress bar what your progress is, and inside that loop you do all your hard work.

This is a common pattern, but it's not compulsory.

You don't have to have a loop inside your DoWork method. You can do whatever you want, reporting progress whenever you want, and your ProgressChanged handler can interpret your progress reports however it wants, not necessarily as a percentage.

Upvotes: 0

Fabian Bigler
Fabian Bigler

Reputation: 10905

There are different ways how to deal with this. I prefer using the DoWorkEventArgs.Argument. So basically pass your list of fileNames to the BackGroundWorker:

 void m_oWorkder_DoWork(object sender, DoWorkEventArgs e)
  {
        var fileNames = (list<string>) e.Argument;

        foreach (var fileName in fileNames)
        {
          //write some stuff, do whatever you wish
          m_oWorker.ReportProgress(fileNames.IndexOf(fileName));
        }
   }

Upvotes: 1

Related Questions