Skip to content

Instantly share code, notes, and snippets.

@melvinlee
Created December 22, 2014 06:30
Show Gist options
  • Save melvinlee/dd5b8032be63495a7276 to your computer and use it in GitHub Desktop.
Save melvinlee/dd5b8032be63495a7276 to your computer and use it in GitHub Desktop.
/**************************************************************************************
Version: 1.0.1
Delegate Extension Methods
***************************************************************************************/
using System;
using System.Windows.Forms;
public static class ControlExtension
{
public static void BeginInvokeAction(this Control control, Action action)
{
if (control.IsDisposed || (!control.IsHandleCreated && !control.FindForm().IsHandleCreated))
{
// some exceptional condition:
// handle in whatever way is appropriate for your app
return;
}
if (control.InvokeRequired)
control.BeginInvoke(new MethodInvoker(action));
else
action.Invoke();
}
public static void BeginInvokeAction(this Control control, params Action[] actions)
{
if (control.IsDisposed || (!control.IsHandleCreated && !control.FindForm().IsHandleCreated))
{
// some exceptional condition:
// handle in whatever way is appropriate for your app
return;
}
foreach (var action in actions)
{
if (control.InvokeRequired)
control.BeginInvoke(new MethodInvoker(action));
else
action.Invoke();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment