后台工作的CancelAsync

后台工作的CancelAsync

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

问题描述

我要中止进程,但不能这样做,我使用的背景工人,我的处理功能。

I want to abort the process but not able to do so, I am using Background worker with my functions of processing.

public void Init()
{
    bw = new BackgroundWorker();
    bw.WorkerSupportsCancellation = true;
    bw.DoWork += new DoWorkEventHandler(bw_DoWork);
    bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
}

void bw_DoWork(object sender, DoWorkEventArgs e)
{
    if (bw.CancellationPending == true)
    {
        e.Cancel = true;
    }
    else
    {
        e.Result = abd();
    }
}

void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    if(e.Cancelled)
    {
        lbltext.content="Canceled";
    }

    else
    {
        lbltext.content="Completed";
    }
}

private void btncan_Click(object sender, RoutedEventArgs e)
{
    bw.CancelAsync();
}

private void btnstart_Click(object sender, RoutedEventArgs e)
{
    bw.RunWorkerAsync();
}



我不能够放弃使用此代码的过程。
功能 ABD()正在执行处理部分和返回结果。

I am not able to abort the process using this code.Function abd() is performing the processing part and returning the result.

请给我提供任何解决方案。

Please provide me any solution.

感谢。

推荐答案

当你调用 bw.CancelAsync()刚才设置 CancellationPending 标志真正。它不取消缺省事。您需要手动处理未决取消。但是,你不能这样做,你的代码,因为当你点击按钮时,有三种可能的选择:

When you call bw.CancelAsync() you just set CancellationPending flag to true. It does not cancels something by default. You need to handle pending cancellation manually. But you can't do that with your code, because when you click button, there are three possible options:


  • 长期运行 ABD()方法,完成了它的工作,并没有要取消

  • ABD()开始它的工作,后台工作受阻 - 它在等待 ABD(),那么继续执行 - 即退出的if-else 块,提高了 RunWorkerCompleted 事件

  • 几乎是不可能的选项 - 你会快如光,你会如果其他块进入前点击按钮。比 CancellationPending 将是真实的,而 ABD()不会开始执行

  • Long-running abd() method finished it's work and there is nothing to cancel
  • abd() started it's work, and background worker is blocked - it's waiting for results of abd(), then it continues execution - i.e. exits if-else block and raises RunWorkerCompleted event.
  • Nearly impossible option - you will be fast as light, and you will click button before if-else block entered. Than CancellationPending will be true, and abd() will not start execution

如果你想使用注销,然后做在一个循环中长时间运行的任务,并检查是否取消被每个待定步:

If you want to use cancellation, then do your long-running task in a loop, and check if cancellation is pending on each step:

void bw_DoWork(object sender, DoWorkEventArgs e)
{
    List<Foo> results = new List<Foo>();

    // any loop here - foreach, while
    for(int i = 0; i < steps_count; i++)
    {
         // check status on each step
         if (bw.CancellationPending == true)
         {
             e.Cancel = true;
             return; // abort work, if it's cancelled
         }

         results.Add(abd()); // add part of results
    }

    e.Result = results; // return all results
}

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

08-18 13:06