public delegate int TakesAWhileDelegate(int data, int ms);
static void Main(string[] args)
{
TakesAWhileDelegate d1 = TakeAWhile; ;
d1.BeginInvoke(1, 3000,TakesAWhileCompleted, d1);
for (int i = 0; i < 100; i++)
{
Console.Write(".");
Thread.Sleep(50);
}
Console.ReadKey();
}
private static void TakesAWhileCompleted(IAsyncResult ar)
{
if (ar == null) throw new ArgumentNullException("ar");
TakesAWhileDelegate d1 = ar.AsyncState as TakesAWhileDelegate;
Trace.Assert(d1 != null, "Invalid object type");
int result = d1.EndInvoke(ar);
Console.WriteLine("result:{0}",result);
}
static int TakeAWhile(int a,int ms)
{
Console.WriteLine("TakesAwhile started");
Thread.Sleep(ms);
Console.WriteLine("TakesAWhile completed");
return ++a;
}
}
在这个方法里d1.BeginInvoke(1, 3000,TakesAWhileCompleted, d1),TakesAWhileCompleted不是需要一个IAsyncResult的参数吗?执行TakesAWhileCompleted方法时那个参数在哪?怎么传进去的?