-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,100
  • 5
  • 21
  • 44
  • 1
    Take a look here: http://stackoverflow.com/questions/5868783/solve-a-cross-threading-exception-in-winforms – Nick Spreitzer Sep 02 '12 at 18:19
  • 6
    There are hundreds, maybe even thousands of questions on SO about the same error. How many did you read before posting? – H H Sep 02 '12 at 18:20

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
  • 46,889
  • 11
  • 103
  • 119