-2

可能重复:
C# 中的“跨线程操作错误”需要帮助
解决 WinForms 中的跨线程异常

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

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

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

我该如何解决?

4

1 回答 1

2

在ui线程中运行命令:

如何从 C# 中的另一个线程更新 GUI?

this.Invoke((MethodInvoker)delegate {
    this.close();
});
于 2012-09-02T18:20:13.863 回答