【问题标题】:Illegal Cross Thread Operation error [duplicate]非法跨线程操作错误[重复]
【发布时间】:2012-09-02 18:17:28
【问题描述】:

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

我尝试在 foreach 循环中添加进度条

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

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

我该如何解决?

【问题讨论】:

标签: c#


【解决方案1】:

在ui线程中运行命令:

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

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

【讨论】:

  • 可能,但 Completed 事件更合适。
猜你喜欢
  • 2012-08-22
  • 1970-01-01
  • 2010-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多