I have simple application. I have an arraylist and this list includes websites.
I have an error when I click the button. The error is ;
A first chance exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll
Additional information: Cross-thread operation not valid: Control 'listBox2' accessed from a thread other than the thread it was created on.
If there is a handler for this exception, the program may be safely continued.
Codes are below:
private void button2_Click(object sender, EventArgs e) {
for (int i = 0; i < sitelist.Count; i++) {
Thread thread = new Thread(getStatus);
thread.Start((string)sitelist[i]);
}
}
private void getStatus(Object obj) {
listBox2.Items.Add("1");
}
When I wrote the code like:
private void getStatus(Object obj) {
MessageBox.Show((string)obj);
}
it works. Why it shows error when I use listbox ?
Sincerely. Ömer.