【问题标题】:Error handling condition using BackGroundWorker Fails使用 BackGroundWorker 失败的错误处理条件
【发布时间】:2015-04-13 13:24:42
【问题描述】:

为了习惯并在我的项目中使用Backgroundworker 进行培训,我使用了一个运行平稳的示例代码,除非在处理错误情况时它不允许我使用RunWorkerCompletedEventArgs e(e.Error!=null) 设施.我想像取消和成功完成一样处理错误。

请给点建议! 以下是代码:

      private void DoWork(object sender, DoWorkEventArgs e)
        {
           Random rand = new Random();
           for (int i = 0; i < 10; i++)
             {
                 if (this.backgroundWorker.CancellationPending)
                  {
                     e.Cancel = true;
                     break;

                  }

                 // report progress
                 this.backgroundWorker.ReportProgress(-1, string.Format("Performing step {0}...", i + 1));

                 // simulate operation step
                System.Threading.Thread.Sleep(rand.Next(1000, 10000));

                //setting simulateError to true after inducing error(a button)
                if (this.simulateError)
                  {
                    this.simulateError = false;
                    //needs a code to use (e.Error!=null) in 
                      RunWorkerCompleted().
                   //A jump to RunWorkerCompleted is required here.

                  }



           }
      }
    private void RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        // hide animation
         this.pictureBox.Image = null;

        // show result indication
        if (e.Cancelled)
        {   
            ////works nice 
            this.labelProgress.Text = "Operation cancelled by the user!";
            this.pictureBox.Image = Properties.Resources.WarningImage;
        }
        else
        {
           //doesn't execute at all....why?

            if (e.Error != null)
            {
                this.labelProgress.Text = "Operation failed: " + e.Error.Message;
                this.pictureBox.Image = Properties.Resources.ErrorImage;
            }
            else
            {
                //works nice
                this.labelProgress.Text = "Operation finished successfuly!";
                this.pictureBox.Image = Properties.Resources.InformationImage;
            }
        }

        // restore button states
        this.buttonStart.Enabled = true;
        this.buttonCancel.Enabled = false;
        this.buttonError.Enabled = false;
    }

我使用simulateError 引发错误,目的是显示特殊的错误消息我应该如何使用

   if (e.Error != null)
            {
                this.labelProgress.Text = "Operation failed: " +  e.Error.Message;

如果出现错误,我的程序不会到达private void RunWorkerCompleted(object sender, ,RunWorkerCompletedEventArgs e)。在其他情况下(取消、成功完成)它会正确执行。

【问题讨论】:

  • 很难看出什么是“失败的”。该代码有一个错误的注释://setting simulateError to true upon cancel_click。注释也描述了代码中实际发生的情况。你检查 CancellationPending 你检查模拟错误,所以你永远不会真正模拟一个错误。
  • 我实际上使用模拟错误点击按钮模拟了一个错误并设置了simulateError=true,因为代码简短而完整,所以我没有在这里显示
  • 为什么这些标签的观看者人数很少!
  • 哪一行抛出异常?您的代码似乎没问题

标签: c# multithreading error-handling backgroundworker c#-5.0


【解决方案1】:

为了习惯并在我的项目中使用 Backgroundworker 接受培训

你为什么要这样做? Task.Run() is better than BackgroundWorker in every way.


现在回到您的实际问题:要在 BackgroundWorker 中导致错误,请使用 .Net 中的标准机制:异常:

if (this.simulateError)
{
    this.simulateError = false;

    throw new Exception("Simulating error.");
}

【讨论】:

    猜你喜欢
    • 2010-10-06
    • 2013-01-25
    • 2011-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-10
    • 1970-01-01
    相关资源
    最近更新 更多