Created
January 4, 2017 20:15
-
-
Save jpreece6/3ea21b9309b30e4e2bfdcb445c40688b to your computer and use it in GitHub Desktop.
Best method for easy invoke operations
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
namespace ExtensionMethods | |
{ | |
public static class ExtensionMethods | |
{ | |
public static void InvokeIfRequired<T>(this T obj, Action<T> action) where T : Control | |
{ | |
if (obj.InvokeRequired) | |
{ | |
obj.Invoke(new Action(() => action(obj))); | |
} | |
else | |
{ | |
action(obj); | |
} | |
} | |
} | |
} | |
// Usage | |
// txtName.InvokeIfRequired(x => { x.Text = 'Naomi' }); | |
// Credit to http://stackoverflow.com/questions/2367718/automating-the-invokerequired-code-pattern |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment