I have the following code that I used to set properties in streaming safe mode (adapted from this other SO question , but I cannot adapt it to get the property.
This is my set property in safe thread code.
public static void SetPropertyThreadSafe(this TControl self, Action setter) where TControl : Control { if (self.InvokeRequired) { var invoker = (Action)(() => setter(self)); self.Invoke(invoker); } else { setter(self); } }
Called as follows:
this.lblNameField.SetPropertyThreadSafe(p => p.Text = "Name:");
This is my attempt to get the property in thread safe code.
public static TResult GetPropertyThreadSafe(this TControl self, Func getter) where TControl : Control { if (self.InvokeRequired) { var invoker = (Func)((TControl control) => getter(self)); return (TResult)self.Invoke(invoker); } else { return getter(self); } }
This does not work. I would hope to call it by doing the following:
string name = this.lblNameField.GetPropertyThreadSafe(p => p.Text);
source share