Monday, April 2, 2012

Cross-thread operation not valid: Control xxx accessed from a thread other than the thread it was created on.

This error is occurring when  you are going to access the UI with another thread. There are method you can use to over come this situation.

The best approach is to use method Invoker to update the UI when it is required.

if (txtDataRx.InvokeRequired)
{
txtDataRx.Invoke(
new MethodInvoker(delegate { txtDataRx.Text = txtDataRx.Text + szData; }));
}
else
{
txtDataRx.Text = txtDataRx.Text + szData;
}

9 comments:

Anonymous said...

Thanks! I've read a bunch of posts about this but yours was the clearest.

Melick Rajee said...

@ Anonymous,

Thanks ..!

Anonymous said...

Great Post, it worked alot.


Danish Ali Khan
NED,Chalmers University
Sweden

Melick Rajee said...

@Danish Ali Khan

Thank you!

Anonymous said...

thanks, it worked for me too

Melick Rajee said...

Thanks Dude !

Anonymous said...

Thanks, I am stuck NO MORE!! Thank you for the answer.

Melick Rajee said...

Thanks Mate!

Anonymous said...

Very straight forward unlike any other blogs that I read. It works.
Good job. Thanks you.