cancel background worker exception in e.result

霸气de小男生 提交于 2019-12-05 01:12:29

问题


i have a serious problem with background worker. code is working if task is ending regular. when i cancel the background task i get an system.invalidoperationexception in the RunWorkerCompleted function for e.Result. what is wrong? thank you.

here is my cod:

private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
  if (backgroundWorker.CancellationPending == true)
    e.Cancel = true;
  e.Result = resultList;
}


private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
  if (e.Error != null)
    List<Object> resultList = (List<Object>)e.Result;
} 

回答1:


This is by design, the Result property getter will throw when DoWork was cancelled or threw an exception. Simply check for that:

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    if (!e.Cancelled && e.Error == null) {
       List<Object> resultList = (List<Object>)e.Result;
       // etc..
    }
} 



回答2:


After a long struggle (being a novice) I managed to incorporate @Hans's suggestion with my code. Hope the following code block helps beginners like me who want to return the currently processed results when cancelasync() is called.

Code block

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.ComponentModel;
using System.Data;

namespace CancelBackgroundWorker
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private BackgroundWorker worker = null;

        public MainWindow()
        {
             InitializeComponent();
                        worker = new BackgroundWorker();
                        worker.WorkerSupportsCancellation = true;
                        worker.WorkerReportsProgress = true;
                        worker.DoWork += worker_DoWork;
                        worker.ProgressChanged += worker_ProgressChanged;
                        worker.RunWorkerCompleted += worker_RunWorkerCompleted;
        }


                private void btnStart_Click(object sender, RoutedEventArgs e)
                {
                        worker.RunWorkerAsync();
                }

                private void btnCancel_Click(object sender, RoutedEventArgs e)
                {
                    worker.CancelAsync();


                }

                void worker_DoWork(object sender, DoWorkEventArgs e)
                {

                    for(int i = 0; i <= 100; i++)
                        {
                                if(worker.CancellationPending == true)
                                {
                                    //http://stackoverflow.com/questions/8300799/cancel-background-worker-exception-in-e-result
                                      //  e.Cancel = true; //This does the trick
                                        e.Result = 100;
                                        return;
                                }
                                worker.ReportProgress(i);
                                System.Threading.Thread.Sleep(250);

                        }
                        e.Result = 42;

                }

                void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
                {
                        lblStatus.Text = "Working... (" + e.ProgressPercentage + "%)";
                }

                void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
                {
                        if(e.Cancelled)
                        {
                                lblStatus.Foreground = Brushes.Red;
                                lblStatus.Text = "Cancelled by user..." + e.Result;

                        }
                        else
                        {
                                lblStatus.Foreground = Brushes.Green;
                                lblStatus.Text = "Done... Calc result: " +e.Result;

                        }
                }

    }
}


来源:https://stackoverflow.com/questions/8300799/cancel-background-worker-exception-in-e-result

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