Last active
January 11, 2024 20:34
-
-
Save maxkoshevoi/c4b6eb535c6924f1d7e7a890a1d10cd4 to your computer and use it in GitHub Desktop.
Simplest way to update UI from another thread in WinForms
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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