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,
28 comments:
this was really useful.
Thanks for an example. I am trying to use this code with my work (button.Enabled = true or false).
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.:)
This post was extremely helpful.
I was going mad by getting that error again and again...
Thank you very much...
thank you very much
THANK YOU!!! Fixed my problem in less than 10 minutes. Extremely useful example.
You're welcome.
THANK YOU SO MUCH. This is exactly what i needed to fix my problem.
Excellent post. Short, succinct and exactly what I was looking for!
Kudos - short, sweet, correct!
Thanks buddy,
this worked for me.
Can you explain in more detail what the issue here is.
regards,
L
Easy to understand and implement - thanks for the example!
Thanks alot, this was magical!
Ended hours of hopelessness
I really like this post, it's short and sweet and solves my problem. Thank-you.
dude, u rocks... this help'd me a lot..P.S thnxx
epic
This was extremely helpful. Thank you for keeping it simple. Awesome job.
I sure wish I'd read this about 8 programming-hours ago. :)
Ati sunder , papa kasam dil khus ho gaya.........
really really thanks man...
i was looking for this code....
great job...
Finally! thank you for this. Helped alot
It's awesome! And it's simple as well. Thank you very much. Save me a lot of time.
Thanks a lot.. Appreciate your good work.. really helpful
this was extremely helpfull...... thnx buddy
Great!!! Simple explanation to a Complex Problem, Very Useful.
Thank You
Good stuff!!!
thnak you! worked like a charm... just a simple working piece of code! cheers
Thank You!!.
Worked like a charm.
Thank you! You are really good man.
Post a Comment