如何关闭BackgroundWorker的线程应用程序时停用

如何关闭BackgroundWorker的线程应用程序时停用

本文介绍了如何关闭BackgroundWorker的线程应用程序时停用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建的BackgroundWorker 线程,在循环我检查每一个时间,如果 CancellationPending 是真还是假,像这样的:

I create thread with BackgroundWorker, and in the loop I check every time if CancellationPending is true or not, like this:

   public MainPage()
    {
        InitializeComponent();

        bw = new BackgroundWorker();
        bw.WorkerReportsProgress = true;
        bw.WorkerSupportsCancellation = true;
        bw.DoWork += new DoWorkEventHandler(bw_DoWork);
        bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
        bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
    }

    private void ButtonStart_Click(object sender, RoutedEventArgs e)
    {
        if (bw.IsBusy != true)
        {
            bw.RunWorkerAsync();
        }
    }

    private void ButtonCancel_Click(object sender, RoutedEventArgs e)
    {
        if (bw.WorkerSupportsCancellation)
        {
            bw.CancelAsync();
        }
    }

    private void bw_DoWork(object sender, DoWorkEventArgs e)
    {
        BackgroundWorker worker = sender as BackgroundWorker;

        for (int i = 1; i <= 100; i++)
        {
            Debug.WriteLine("The tread is working");
            if (worker.CancellationPending)
            {
                e.Cancel = true;
                bw.CancelAsync();
                break;
            }
            else
            {

                System.Threading.Thread.Sleep(500);
                worker.ReportProgress(i);
            }
        }
    }

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

    private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        tbProgress.Text = e.ProgressPercentage.ToString() + "%";
    }

当应用程序被停用,线程未关闭时,它会被中止,发生异常。如何关闭线程与的BackgroundWorker 当应用程序被停用?

When application is deactivated, the thread wasn't closed, it is aborted and the exception occurs. How close threads with BackgroundWorker when application is deactivated?

推荐答案

你有没有将的BackgroundWorker 要取消?

 var bg= new BackgroundWorker();
 bg.WorkerSupportsCancellation = true;



从文档:

From the documentation:

如果您希望的BackgroundWorker支持取消将 WorkerSupportsCancellation 属性设置为true。当此属性为true,可以调用 CancelAsync 方法中断后台操作。

此外,您的代码似乎是错的,你应该叫 CancelAsync()的你的线程代码之外,这将设置 CancellationPending 标志,你可以用它来退出循环。

Also your code seems to be wrong, you should call the CancelAsync() outside of your thread code, this will set the CancellationPending flag that you can use to exit the loop.

虽然我不是100%肯定,因为我不知道在哪里体重变量是从哪里来的。
`

Although I'm not 100% sure as I don't know where the bw variable is coming from.`

这篇关于如何关闭BackgroundWorker的线程应用程序时停用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 06:20