Friday, October 9, 2009

InvalidOperationException: Cross-thread operation not valid: Control ‘Form1’ accessed from a thread other than the thread it was created on.

When I first time met with this error, it was caused by an update of a windows form control from a separate thread.

Here is the workaround.

// delegation to cross the thread boundary
private delegate void AddComboBoxItemDelegate(object item);

private void AddComboBoxItem(object item)
{
  if (this.comboBox1.InvokeRequired) {
    // Invoke delegate so that the main UI thread is able to update the controls in the form
this.comboBox1.Invoke(new AddComboBoxItemDelegate(this.AddComboBoxItem), item);
  }
  else {
    this.comboBox1.Items.Add(item);
  }
}



Hope this helps. Cheers!

6 comments:

Anonymous said...

Thank you owing ideas))).

Anonymous said...

Just wanted to say hello all. This is my first post.

I hope to learn a lot here.

Anonymous said...

Just wanted to say hello to all here. This is my 1st posting here.

It definitely seems like the moderators of this forum sure are doing a marvellous job.

I hope to find alot of info here also I will assist other people as much as I can.

Anonymous said...

Aloha i'm fresh on here, I came accross this forum I find It extremely accommodating & it has helped me a lot. I hope to give something back & help others like its helped me.

Cheers all, Catch You About.

Anonymous said...

Sup i am new to this, I came upon this board I have found It absolutely accessible and it has helped me so much. I should be able to contribute & guide others like its helped me.

Thanks, Catch You Later

Anonymous said...

Man .. Excellent .. Amazing .. I will bookmark your web site and take the feeds alsoI'm glad to seek out a lot of useful info right here within the publish, we'd like work out more strategies in this regard, thanks for sharing. . . . . .

 

Get paid for your opinions! Click on the banner above to join Planet Pulse. Its totally free to sign up, and you can earn UNLIMITED. Find out more by visiting PLANET PULSE.
July Code Blog Copyright © 2010 Blogger Template Designed by Bie Blogger Template