-2

Possible Duplicate:
Help needed for 'cross-thread operation error' in C#
Solve a cross-threading Exception in WinForms

I've tried to add progressbar in the foreach-loop

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

now i have an Illegal Cross Thread Operation error in this.close(); line

How can i solve it?

Community
  • 1
  • 1
fen1ksss
  • 1,050
  • 5
  • 20
  • 44

1 Answers1

2

Run the command in the ui thread:

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

this.Invoke((MethodInvoker)delegate {
    this.close();
});
Community
  • 1
  • 1
Petar Minchev
  • 45,963
  • 11
  • 102
  • 118