非法交叉线程操作错误

时间:2012-09-02 18:17:28

标签: c#

  

可能重复:
  Help needed for 'cross-thread operation error' in C#
  Solve a cross-threading Exception in WinForms

我尝试在foreach-loop中添加进度条

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    foreach (...)
    {
        ...
        i++;
        backgroundWorker1.ReportProgress(100 * i / rcount);
        Thread.Sleep(100);
    }
    this.close();
}

现在我有一个非法交叉线程操作错误 this.close();  线

我该如何解决?

1 个答案:

答案 0 :(得分:2)

在ui线程中运行命令:

How to update the GUI from another thread in C#?

this.Invoke((MethodInvoker)delegate {
    this.close();
});