简体   繁体   中英

How do I use backgroundworker in this instance?

I've a question on how the backgroundworker would actually work. I'm not too sure how it would work with different methods.

So for example I have the following code (adapted from http://broadcast.oreilly.com/2010/06/understanding-c-using-backgrou.html for illustration purposes):

private void getDateTime()
{
    DateTime startTime = DateTime.Now;
    double value = Math.E;
    while (DateTime.Now < startTime.AddMilliseconds(100)) 
    {
      value /= Math.PI;
      value *= Math.Sqrt(2);
    }
}

private backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    for(int i=0; i <= 100; i++)
    {
        getDateTime();
        backgroundWorker1.ReportProgress(i);
    }
}

This would continually calculate the DateTime using the backgroundworker thread. Now I'm assuming that the for-loop within the backgroundworker's DoWork method would run the calculation, report the progress, and then reiterate the loop (please do correct me if I've misunderstood).

Now assuming I have another method, which parses a (presumably large) DataTable and returns another DataTable, using it in the backgroundworker would mean I'd be parsing the DataTable over and over (theoretically. again, correct me if I'm wrong)? If this is how the program would flow, is there a more efficient method to achieve the DataTable parsing without having to reiterate the loop, but still being able to use backgroundworker?

This is intended for a Windows Form, and the UI tends to freeze up when I parse the DataTable(s). I've heard that backgroundworker could help me get around the freezing of the UI, but I find it highly inefficient to have to re-parse the entire DataTable over, and over. However I'm not sure of any other way I can report progress without a for-loop either.

EDIT:

I've figured out a temporary solution. It works, but I still have no way of reporting progress.

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        int rows = (int)e.Argument;
        int count = 0;
        t3 = runComparison();
        for (int i = 0; i <= rows; i++)
        {
            count++;
            int current = count / rows;
            backgroundWorker1.ReportProgress(current * 100);
        }
        e.Result = t3;
    }

    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        progressBar1.Value = e.ProgressPercentage;
    }

    private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        if (e.Error != null)
        {
            MessageBox.Show("An unexpected error has occurred. Please try again later.", "ERROR!", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        else
        {
            dataGridView1.DataSource = e.Result;
        }
    }

I'm aware my for-loop doesn't do anything. However, the "runComparison" already consumes a lot of time. As such, progress doesn't get reported until an entire run-through if it's in the for-loop. Alternatively, maybe I could use a label or messagebox instead of the progress bar. Not too sure how I'm supposed to achieve this.

Try to do this:

//make sure background worker report progress
backgroundWorker1.WorkerReportsProgress = True;


private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    progressBar1.Value = e.ProgressPercentage;
    progressBar1.Refresh;
    //if your progressbar is on a status strip, you need to refresh the status strip instead
}

I've come up with a rather inelegant solution. But it satisfies the requirement for user-friendliness so... Whatever works :P

Thanks for all the feedback, I appreciate it.

Anyway, here's the code:

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        backgroundWorker1.ReportProgress(1); //to invoke ProgressChanged
        t3 = runComparison(); //run intensive method
        e.Result = t3; //return DataTable
    }

    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        progressBar1.Visible = true; //display a label
        textBoxProgress.Visible = true; //display the progress bar
        //change the progress bar style
        //the Marquee style is constantly running, so it could show users the process is working
        //not too sure how to explain it, you'll have to try it out. 
        progressBar1.Style = ProgressBarStyle.Marquee; 
    }

    private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        if (e.Error != null)
        {
            MessageBox.Show("An unexpected error has occurred. Please try again later.", "ERROR!", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        else
        {
            //reset the display and update the DataGridView
            progressBar1.Visible = false;
            textBoxProgress.Visible = false;
            progressBar1.Style = ProgressBarStyle.Blocks;
            dataGridView1.DataSource = e.Result;
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM