More Multithread and UI Forms Update

  • Thread starter Thread starter John Bowman
  • Start date Start date
J

John Bowman

Hi,

I've read "Cybertof"'s thread (04/19/04) & the article he provided in a link
about accessing UI elements from a thread that did not create them. What the
authors are saying makes plenty of sense. However, using the control's
Invoke method handles calling it's methods, but what about accessing the
control's properties from another thread? What's the proper way to do that?
 
I've read "Cybertof"'s thread (04/19/04) & the article he provided in a link
about accessing UI elements from a thread that did not create them. What the
authors are saying makes plenty of sense. However, using the control's
Invoke method handles calling it's methods, but what about accessing the
control's properties from another thread? What's the proper way to do that?

Again, through a delegate and Invoke. You shouldn't access the property
directly from a non-UI thread.
 
Jon,

Thanks for the quick reply. I kind of suspected that would be the answer
<g>. However, I don't understand (code wise) how to use Invoke to access a
property. I understand how to use it for accessing a method. Do you just
treat the property of interest as if it's a method that has no arguments?
Any example code?

TIA,

John
 
Thanks for the quick reply. I kind of suspected that would be the answer
<g>. However, I don't understand (code wise) how to use Invoke to access a
property. I understand how to use it for accessing a method. Do you just
treat the property of interest as if it's a method that has no arguments?

No - you would have to create a delegate which accessed the property,
and invoke that delegate.
Any example code?

Well, just:

delegate string StringReturningDelegate();

string GetLabelText()
{
return label.Text;
}

....

myControl.Invoke
(new StringReturningDelegate(myControl.GetLabelText());

should work.
 
Hi John,

You can use a method to access a property. This would be a special method
that you call thru Invoke and this method sets or gets the property. The
other way is to create a delegate to invoke the 'get' or 'set' accessor of
the property directly. Hoewever IMHO the first is more easy to use. For
invoking the accessor directly you can follow this link
http://groups.google.ca/groups?hl=e...F-8&oe=UTF-8&q=delegate+set+accessor+stoitcho
Look for my post, which is last in the thread.


--
HTH
B\rgds
Stoitcho Goutsev (100) [C# MVP]

John Bowman [email protected]> said:
Jon,

Thanks for the quick reply. I kind of suspected that would be the answer
<g>. However, I don't understand (code wise) how to use Invoke to access a
property. I understand how to use it for accessing a method. Do you just
treat the property of interest as if it's a method that has no arguments?
Any example code?

TIA,

John


a
What
 
Jon & Stoitcho,

Thanks for the help. I should be able to continue from here.

John


Stoitcho Goutsev (100) said:
Hi John,

You can use a method to access a property. This would be a special method
that you call thru Invoke and this method sets or gets the property. The
other way is to create a delegate to invoke the 'get' or 'set' accessor of
the property directly. Hoewever IMHO the first is more easy to use. For
invoking the accessor directly you can follow this link
http://groups.google.ca/groups?hl=e...F-8&oe=UTF-8&q=delegate+set+accessor+stoitcho
Look for my post, which is last in the thread.


--
HTH
B\rgds
Stoitcho Goutsev (100) [C# MVP]

John Bowman [email protected]> said:
Jon,

Thanks for the quick reply. I kind of suspected that would be the answer
<g>. However, I don't understand (code wise) how to use Invoke to access a
property. I understand how to use it for accessing a method. Do you just
treat the property of interest as if it's a method that has no arguments?
Any example code?

TIA,

John
in
 
Back
Top