Wednesday, July 16, 2008

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

This exception is thrown when you try to access control from thread other than the thread it was created on.
To access control and control properties use delegate. Its easy. Here is example for accessing Label control (label1) Text property.


delegate void updateLabelTextDelegate(string newText);
private void updateLabelText(string newText)
{
if (label1.InvokeRequired)
{
// this is worker thread
updateLabelTextDelegate del = new updateLabelTextDelegate(updateLabelText);
label1.Invoke(del, new object[] { newText });
}
else
{
// this is UI thread
label1.Text = newText;
}
}


Now whenever you want to change text of label you call method updateLabelText(newText). You can do the same for all other controls.

keys: gui multithreading, update from another thread, threading, cross-thread,

27 comments:

ashish said...

this was really useful.

JC said...

Thanks for an example. I am trying to use this code with my work (button.Enabled = true or false).

bharat khiani said...

a really good way. i had 2 threads, 1 for UI and one for bringing data thru sockets. my second thread was able to call the public delegated method this way. tx.:)

Saurabh said...

This post was extremely helpful.

I was going mad by getting that error again and again...

Thank you very much...

Anonymous said...

thank you very much

Anonymous said...

THANK YOU!!! Fixed my problem in less than 10 minutes. Extremely useful example.

Niko said...

You're welcome.

Anonymous said...

THANK YOU SO MUCH. This is exactly what i needed to fix my problem.

Anonymous said...

Excellent post. Short, succinct and exactly what I was looking for!

Chris Grenard said...

Kudos - short, sweet, correct!

Anonymous said...

Thanks buddy,

this worked for me.
Can you explain in more detail what the issue here is.

regards,

L

tking2097 said...

Easy to understand and implement - thanks for the example!

Joachim said...

Thanks alot, this was magical!

Ended hours of hopelessness

Trevor said...

I really like this post, it's short and sweet and solves my problem. Thank-you.

Anonymous said...

dude, u rocks... this help'd me a lot..P.S thnxx

Anonymous said...

epic

Anonymous said...

This was extremely helpful. Thank you for keeping it simple. Awesome job.

I sure wish I'd read this about 8 programming-hours ago. :)

shabir said...

Ati sunder , papa kasam dil khus ho gaya.........

Cokiki said...

really really thanks man...
i was looking for this code....
great job...

Anonymous said...

Finally! thank you for this. Helped alot

Anonymous said...

It's awesome! And it's simple as well. Thank you very much. Save me a lot of time.

Sanjeev said...

Thanks a lot.. Appreciate your good work.. really helpful

asif ali said...

this was extremely helpfull...... thnx buddy

Prakash B said...

Great!!! Simple explanation to a Complex Problem, Very Useful.
Thank You

Anonymous said...

Good stuff!!!

Anonymous said...

thnak you! worked like a charm... just a simple working piece of code! cheers

Mohamed said...

Thank You!!.
Worked like a charm.