I am working with Windows Forms, and many times have bumped into (as I understood it) the necessity to write wrapping functions around properties of the UI components, so that they (the properties) could be set from another thread by invoking their wrappers.
However, one thing doesn't give me rest. Are not the setters of properties actually functions themselves? If they are, could a Delegate be formed around them without resorting to writing wrappers, and then said delegate be invoked from another thread?
Yes, this is possible. Use the PropertyInfo.GetSetMethod
function to retrieve the set accessor for the property, and then create a delegate to invoke it.
Or even simpler yet, you could use the PropertyInfo.SetValue
function to set the value directly.
I can't find the real solution for this question but I figured it out. I sometimes need it myself but Google fails me.
Example:
public string Blah
{
get
{
if (InvokeRequired)
return (string) Invoke(new Func<string>(() => Blah));
else
return Text;
}
set
{
if (InvokeRequired)
Invoke(new Action(() => { Blah = value; }));
else
Text = value;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With