C#, Background worker

送分小仙女□ 提交于 2020-01-24 09:37:12

问题


I have a sample WinForms application that uses the BackgroundWorker component. It works fine but when I hit the Cancel button to cancel the background thread it doesn't cancel the thread. When I hit the Cancel button to call .CancelAsync() method, then in RunWorkerCompleted event handler e.Cancelled Property always remains false. I think when I hit Cancel button it should be set to true.

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    for (int i = 1; i <= 100; i++)
    {
       // Wait 100 milliseconds.
       Thread.Sleep(100);
       // Report progress.
       if (backgroundWorker1.CancellationPending == true)
       {
           //label1.Text = "Cancelled by user.";
           break;
        }

        backgroundWorker1.ReportProgress(i);
     }
}

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    // Change the value of the ProgressBar to the BackgroundWorker progress.
    progressBar1.Value = e.ProgressPercentage;
    // Set the text.
    label1.Text = e.ProgressPercentage.ToString();
}

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    if (e.Cancelled == true)
    {
        label1.Text = "Canceled!";
    }
    else if (e.Error != null)
    {
        label1.Text = "Error: " + e.Error.Message;
    }
    else
    {
         label1.Text = "Done!";
    }
}

private void button2_Click(object sender, EventArgs e)
{
    if (backgroundWorker1.WorkerSupportsCancellation == true)
    {
        // Cancel the asynchronous operation.
        backgroundWorker1.CancelAsync();
    }
}

回答1:


The Cancelled property is still false because you break out of the loop and then allow the backgroundworker's DoWork function to end in the normal way. You never tell your backgroundworker component that the pending cancel was actually accepted.

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    for (int i = 1; i <= 100; i++)
    {
        // Wait 100 milliseconds.
        Thread.Sleep(100);

        if (backgroundWorker1.CancellationPending)
        {
            e.Cancel = true;
            break;
        }

        // Report progress.
        backgroundWorker1.ReportProgress(i);
    }
}

The distinction is important, because sometimes you may want rollback work you've already done when you detect the CancellationPending request, and so it may be a while before you actually finish with the cancel.



来源:https://stackoverflow.com/questions/7471470/c-background-worker

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