BackgroundWorker:RunWorkerCompleted中的InvalidOperationException

问题描述 投票:6回答:6

我有一个带有backgroundWorker的WinForm:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SeoTools.Utils;

namespace SeoTools.UI
{
    public partial class UIProgress : Form
    {
        public UIProgress(DoWorkEventHandler doWorkEventHandler, RunWorkerCompletedEventHandler runWorkerCompletedEventHandler)
        {
            InitializeComponent();
            this.backgroundWorker.WorkerReportsProgress = true;
            this.backgroundWorker.WorkerSupportsCancellation = true;
            this.backgroundWorker.DoWork += doWorkEventHandler;
            this.backgroundWorker.RunWorkerCompleted += runWorkerCompletedEventHandler;
        }

        public void Start()
        {
            var foo = SynchronizationContext.Current;
            backgroundWorker.RunWorkerAsync();
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            btnStop.Enabled = false;
            btnStop.Text = "Stopping...";
            backgroundWorker.CancelAsync(); 
        }

       private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        try
        {
            wdgProgressBar.Value = e.ProgressPercentage;
            if (this.Visible == false)
            {
                this.ShowDialog();
                this.Update();
            }
        }
        catch (InvalidOperationException) {} 
    }

        private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this.Hide(); //Here I get a InvalidOperationException
            this.Dispose();
        }    
    }
}

我第一次运行此程序,效果很好。但是第二次调用InvalidOperationException时得到this.Hide()

“其他信息:跨线程操作无效:控制'UIProgress'是从创建该线程的线程之外的线程访问的。”

奇怪的是,首先在Start()中运行foo是WindowsFormsSyncronizationContext,但是第二次尝试是System.Threading.SyncronizationContext

我正在编写的应用程序是ExcelDna插件。

编辑

Start()的调用方式如下:

 UIProgress uiProgress = new UIProgress(
                delegate(object sender, DoWorkEventArgs args)
                {
                   ....
                },
                delegate(object sender, RunWorkerCompletedEventArgs args)
                    {
                       ...
                    }
            );
            uiProgress.Start();
c# multithreading winforms excel-dna
6个回答
7
投票
从UI线程上运行的代码中调用

您的Start()方法必须”,以允许BackgroundWorker正确运行。并不是当您收到此异常时。在您的方法中添加保护性代码,以便您可以诊断出此事故:

    public void Start()
    {
        if (Thread.CurrentThread.GetApartmentState() != ApartmentState.STA) {
            throw new InvalidOperationException("Bug! Code called from a worker thread");
        }
        backgroundWorker.RunWorkerAsync();
    }

现在,您可以在throw语句上设置断点,并使用调试器的Call Stack窗口找出发生这种情况的原因。


3
投票

您正在后台线程上调用UI操作。这就是该例外的原因。我将使用完全不同的方法来使进度表成为最佳方法,就是将Task与IProgress一起使用。使用它的另一种方式:

private void backgroundWorker_ProgressChanged( object sender , ProgressChangedEventArgs e )
    {

      this.UpdateOnMainThread(
        ( ) =>
        {
          wdgProgressBar.Value = e.ProgressPercentage;
          if ( this.Visible == false )
          {
            this.ShowDialog( );
            this.Update( );
          }
        } );
    }

    private void UpdateOnMainThread( Action action )
    {
      if ( this.InvokeRequired )
      {
        this.BeginInvoke( ( MethodInvoker ) action.Invoke);
      }
      else
      {
        action.Invoke( );
      }
    }

    private void backgroundWorker_RunWorkerCompleted( object sender , RunWorkerCompletedEventArgs e )
    {
      this.UpdateOnMainThread(
        ( ) =>
        {
          this.Hide( ); //Here I get a InvalidOperationException
          this.Dispose( );
        } );

    }

1
投票

在表单上使用BeginInvoke()方法:

// http://msdn.microsoft.com/en-us/library/0b1bf3y3(v=vs.110).aspx

    private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        this.BeginInvoke(new InvokeDelegate(InvokeMethod));               
    }

    public delegate void InvokeDelegate();

    public void InvokeMethod()
    {
        this.Hide(); 
        this.Dispose();
    }

1
投票

我想您可以在这里找到一些帮助:BackgroundWorker hide form window upon completion。但是,不要忘了分离BackgroundWorker事件并自行停止BackgroundWorker,就像在此解释的那样:Proper way to Dispose of a BackGroundWorker。问题可以在

this.Dispose();

backgroundWorker_RunWorkerCompleted事件中。这样您就可以配置表单页面。那是你想做的吗?或者您想处置BackgroundWorker?处置表单页面会释放所有资源,因此第二次执行this.Hide();可能是错误的。

有关详细信息,您可以看到以下链接:C# Form.Close vs Form.DisposeForm.Dispose Method


1
投票

您必须检查此链接How to update the GUI from another thread in C#?

可能有所有可能的答案

希望此帮助


1
投票

您正在从无法操纵UI的线程运行对主线程的调用。最简单的方法是使用匿名委托调用。

更改此:

        if (this.Visible == false)
        {
            this.ShowDialog();
            this.Update();
        }

为此:

this.Invoke((MethodInvoker) delegate { 
        if (this.Visible == false)
        {
            this.ShowDialog();
            this.Update();
        }    
});

这不是最优化的方法,但是不需要太多重新编码就可以非常快地完成工作。 :)

© www.soinside.com 2019 - 2024. All rights reserved.