异步调用过程
时间:2010-11-17 来源:nametmp
2、在委托中加入函数:public int AsynCompute(ulong l, out ulong factorial)
AsynComputeCaller caller = new AsynComputeCaller(AsynCompute);
3、开始异步调用
ulong l = 30;
ulong lf;
IAsyncResult iResult = caller.BeginInvoke(l, out lf, null, null);
4.1、用IsCompleted
while (iResult.IsCompleted == false)
{
// 主线程进行一些操作
Thread.Sleep(10);
}
4.2、WaitOne(单个结果)
// 主线程进行一些操作
System.Threading.WaitHandle waiHand = iResult.AsyncWaitHandle;
waiHand.WaitOne();
4.3、WaitAll、WaitAny(多个结果)
// 主线程进行一些操作
System.Threading.WaitHandle.WaitAll(new System.Threading.WaitHandle[] { waiHand 1,waiHand 2});
WaitHandle.WaitAny(waitHandles,60000, false);
5、获取异步结果
int returnValue = caller.EndInvoke(out lf,iResult);