简化异步委托调用类
如果想异步调用一个方法需要定义相关参数的委托,有时想起来挺麻烦了。所以干脆自己简单的类来简化一下。
public delegate void AsyncDelegate<T>(T t);
public delegate void AsyncDelegate<T, T1>(T t, T1 t1);
public delegate void AsyncDelegate<T, T1, T2>(T t, T1 t1, T2 t2);
public delegate void AsyncDelegate<T, T1, T2, T3>(T t, T1 t1, T2 t2, T3 t3);
public delegate void AsyncDelegate<T, T1, T2, T3, T4>(T t, T1 t1, T2 t2, T3 t3, T4 t4);
public static void Action<T, T1, T2>(T t, T1 t1, T2 t2, AsyncDelegate<T,T1,T2> handler)
{
handler.BeginInvoke(t, t1,t2, ac =>
{
GetEndHandler<AsyncDelegate<T, T1,T2>>(ac).EndInvoke(ac);
}, null);
}
public static void Action<T, T1, T2, T3>(T t, T1 t1, T2 t2, T3 t3, AsyncDelegate<T,T1,T2,T3> handler)
{
handler.BeginInvoke(t, t1,t2,t3, ac =>
{
GetEndHandler<AsyncDelegate<T, T1,T2,T3>>(ac).EndInvoke(ac);
}, null);
}
public static void Action<T, T1, T2, T3, T4>(T t, T1 t1, T2 t2, T3 t3, T4 t4, AsyncDelegate<T, T1, T2, T3,T4> handler)
{
handler.BeginInvoke(t, t1,t2,t3,t4, ac =>
{
GetEndHandler<AsyncDelegate<T, T1,T2,T3,T4>>(ac).EndInvoke(ac);
}, null);
}
static T GetEndHandler<T>(IAsyncResult e)
{
return (T)((AsyncResult)e).AsyncDelegate;
}