ControlsUtils.SetControlPropertyThreadSafeDelegate: Method to change properties of controls in a thread safe way.

This commit is contained in:
2020-07-20 20:50:27 +02:00
parent b9902f4847
commit 8e957dc054

View File

@@ -1,4 +1,5 @@
using System.Windows.Forms;
using System.Reflection;
using System.Windows.Forms;
namespace VAR.Toolbox.Controls
{
@@ -8,5 +9,32 @@ namespace VAR.Toolbox.Controls
{
return size * 96f / ctrl.CreateGraphics().DpiX;
}
private delegate void SetControlPropertyThreadSafeDelegate(
Control control,
string propertyName,
object propertyValue);
public static void SetControlPropertyThreadSafe(
Control control,
string propertyName,
object propertyValue)
{
if (control.InvokeRequired)
{
control.Invoke(new SetControlPropertyThreadSafeDelegate
(SetControlPropertyThreadSafe),
new object[] { control, propertyName, propertyValue });
}
else
{
control.GetType().InvokeMember(
propertyName,
BindingFlags.SetProperty,
null,
control,
new object[] { propertyValue });
}
}
}
}