Skip to content

Instantly share code, notes, and snippets.

@walkure
Created June 2, 2014 15:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save walkure/62ef05e33016b9439834 to your computer and use it in GitHub Desktop.
Save walkure/62ef05e33016b9439834 to your computer and use it in GitHub Desktop.
スタックトレースを握りつぶさないよう頑張ってみたControl.Invoke拡張メソッド cf. http://www2.hatenadiary.jp/entry/2014/06/03/003344
public static class ControlInvokerEx
{
public static object InvokeEx(this Control ctl, Delegate act)
{
return InvokeEx(ctl,act,null);
}
public static object InvokeEx(this Control ctl, Delegate act,params object[] args)
{
if (!ctl.InvokeRequired)
return act.DynamicInvoke(args);
Exception exp = null;
var ret = ctl.Invoke((MethodInvoker)(() =>
{
try
{
act.DynamicInvoke(args);
}
catch (Exception ex)
{
exp = ex;
}
}));
if (exp != null)
throw new System.Reflection.TargetInvocationException(exp);
return ret;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment