Background worker - report progress with string array

北战南征 提交于 2019-11-27 18:17:17

问题


I need to return multiple STRING values from my backgroundworker in each loop, so I tried to use ReportProgress second parameter as string array. Example of code:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    string[] workerResult = new string[2];
    for (int i=0; i<someNumber; i++)
    {
        //do some heavy calculating
        workerResult[0] = "this string";
        workerResult[1] = "some other string";
        backgroundWorker1.ReportProgress(i, workerResult) // also tried workerResult[] and [2]
    }
}

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    string[] results = (string[])e.UserState;

    MessageBox.Show(results[0]); // line of error
    MessageBox.Show(results[1]); // line of error
}

It compiles, but on runtime in the moment I try to access Userstate returned string, I get an error: "Object reference not set to an instance of an object."

For me it seems lik something is wrong when passing array parameter to ProgressChanged delegate, or in ProgressChanged method when trying to set results array values.


回答1:


Your code snippet is incapable of reproducing the problem. A standard mistake is to call ReportProgress() and then to continue modifying the object. It takes a while for the event handler to run, it will see the modified object, not the original. You avoid this by simply creating a new object so that the event handler always works with the original. Like this:

        //do some heavy calculating
        for (int i = 0; i < 2; ++i) {
            string[] workerResult = new string[2];
            workerResult[0] = "this string";
            workerResult[1] = "some other string";
            backgroundWorker1.ReportProgress(i, workerResult);
        }

Note how the array creation statement is moved inside the loop.




回答2:


when you instanciate your BackgroundWorker, you'll have to set reportprogress to true:

worker = new BackgroundWorker { WorkerReportsProgress = true };

On the do work method you'll just need this:

 worker.ReportProgress(10, "Message");

Then something like this to catch the progress:

private void WorkerProgressChanged(object sender, ProgressChangedEventArgs e) {
            if (e.UserState != null) {
                MessageBox.Show(e.UserState);
            }
        }



回答3:


I've solved a similar issue by adding a new ProgressChanged event listener to your background worker and block passing to the next loop if ProgressChanged not fired:

bool progressed;

backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
string[] workerResult = new string[2];
for (int i = 0; i < 2; ++i) {
    progressed=true;

    workerResult[0] = "this string";
    workerResult[1] = "some other string";
    backgroundWorker1.ReportProgress(i, workerResult);
    while (progressed)
    {
       //you can add a thread sleep 
    }
}


void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
     progressed = false;
}


来源:https://stackoverflow.com/questions/5459205/background-worker-report-progress-with-string-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!