C#,后台工作者 [英] C#, Background worker

查看:89
本文介绍了C#,后台工作者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用BackgroundWorker组件的示例WinForms应用程序.它工作正常,但是当我按下Cancel按钮取消后台线程时,它并没有取消线程.当我按下Cancel按钮调用.CancelAsync()方法时,则在RunWorkerCompleted事件处理程序e.Cancelled中,属性始终保持为false.我认为当我按下Cancel按钮时,应该将其设置为true.

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();
    }
}

推荐答案

Canceled属性仍然为false,因为您跳出了循环,然后允许后台工作程序的DoWork函数以正常方式结束.您永远不会告诉后台工作人员组件实际上已接受了挂起的取消.

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);
    }
}

区别很重要,因为有时您可能希望在检测到CancellationPending请求时就已经完成了回滚工作,因此可能需要一段时间才能真正完成取消.

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.

这篇关于C#,后台工作者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆