C# 非法跨线程操作错误

C# 非法跨线程操作错误,c#,C#,可能重复: 我尝试在foreach循环中添加progressbar private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { foreach (...) { ... i++; backgroundWorker1.ReportProgress(100 * i / rcount); Thread.Sleep(100);

可能重复:

我尝试在foreach循环中添加progressbar

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    foreach (...)
    {
        ...
        i++;
        backgroundWorker1.ReportProgress(100 * i / rcount);
        Thread.Sleep(100);
    }
    this.close();
}
现在我有一个非法的跨线程操作错误
this.close()
线


如何解决此问题?

在ui线程中运行命令:


看看这里:关于同一个错误有数百个,甚至数千个问题。在发布之前你读了多少?可能,但完成的事件更合适。
this.Invoke((MethodInvoker)delegate {
    this.close();
});