Skip to content

Instantly share code, notes, and snippets.

@jpreece6
Created January 4, 2017 20:15
Show Gist options
  • Save jpreece6/3ea21b9309b30e4e2bfdcb445c40688b to your computer and use it in GitHub Desktop.
Save jpreece6/3ea21b9309b30e4e2bfdcb445c40688b to your computer and use it in GitHub Desktop.
Best method for easy invoke operations
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