private delegate void SetPropertyValueCallback<ControlType, PropertyType>(ControlType control, string propertyName, PropertyType value) where ControlType : Control;
private void SetPropertyThreadSafe<ControlType, PropertyType>(ControlType control, string propertyName, PropertyType value) where ControlType : Control
{
if (control.InvokeRequired)
{
SetPropertyValueCallback<ControlType, PropertyType> cb = new SetPropertyValueCallback<ControlType, PropertyType>(SetPropertyThreadSafe);
control.Invoke(cb, new object[]{ control, propertyName, value });
}
else
{
System.Reflection.PropertyInfo property = control.GetType().GetProperty(propertyName);
property.SetValue(control, value, null);
}
}
//Anwendung
SetPropertyThreadSafe<ProgressBar, int>(pbState, "Value", (int)syncState.PercentFinished * 100);
SetPropertyThreadSafe<Label, string>(lblState, "Text", syncMessage);