Skip to content

Instantly share code, notes, and snippets.

@maxkoshevoi
Last active January 11, 2024 20:34
Show Gist options
  • Save maxkoshevoi/c4b6eb535c6924f1d7e7a890a1d10cd4 to your computer and use it in GitHub Desktop.
Save maxkoshevoi/c4b6eb535c6924f1d7e7a890a1d10cd4 to your computer and use it in GitHub Desktop.
Simplest way to update UI from another thread in WinForms
myControl.Invoke((Action)delegate
{
myControl.Enabled = true;
});
// More correct way
public static class ControlEx
{
public static void Invoke(this Control control, MethodInvoker action)
{
if (!control.IsHandleCreated)
{
return;
}
if (control.InvokeRequired)
{
control.Invoke(action);
}
else
{
action();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment