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:

  1. Thank you owing ideas))).

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

    I hope to learn a lot here.

    ReplyDelete
  3. 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.

    ReplyDelete
  4. 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.

    ReplyDelete
  5. 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

    ReplyDelete
  6. 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. . . . . .

    ReplyDelete