您的位置:首页 > 编程语言

使用.NET 异步编程(2)

2002-04-22 09:39 501 查看

异步委托

异步委托提供以异步方式调用同步方法的能力。当同步调用一个委托时,调用方法直接对当前线程调用目标方法。如果编译器支持异步委托,则它将生成该调用方法以及BeginInvokeEndInvoke方法。如果调用BeginInvoke方法,则公共语言运行库将对请求进行排队并立即返回到调用方。将对来自线程池的线程调用该目标方法。提交请求的原始线程自由地继续与目标方法并行执行,该目标方法是对线程池线程运行的。如果已经对BeginInvoke指定了回调,当目标方法返回时将调用它。在回调中,使用EndInvoke方法来获取返回值和输入/输出参数。如果没有对BeginInvoke指定回调,则可以在提交请求的原始线程上使用EndInvoke

使用委托

对于异步编程,如果调用方使用一个委托,则该调用方在调用一个方法时必须定义该委托。在以下代码示例中,首先定义该委托,接着创建该委托的实例,然后调用它。下面的示例说明为异步调用Factorize方法定义一个模式的调用方:
usingSystem;
usingSystem.Runtime.Remoting;
publicdelegateboolFactorizingCallback(
intfactorizableNum,
refintprimefactor1,
refintprimefactor2);
//Thisisaclassthatreceivesacallbackwhenthetheresultsareavailable.
publicclassProcessFactorizedNumber
{
privateint_ulNumber;
publicProcessFactorizedNumber(intnumber)
{
_ulNumber=number;
}
//Notethequalifierone-way.
[OneWayAttribute()]
publicvoidFactorizedResults(IAsyncResultar)
{
intfactor1=0,factor2=0;
//ExtractthedelegatefromtheAsyncResult.
FactorizingCallbackfd=
(FactorizingCallback)((AsyncResult)ar).AsyncDelegate;
//Obtaintheresult.
fd.EndInvoke(reffactor1,reffactor2,ar);
//Outputresults.
Console.WriteLine("OnCallBack:Factorsof{0}:{1}{2}",
_ulNumber,factor1,factor2);
}
}
AsynchronousVariation1–call
//TheAsynchronousVariation1call,calls
//theProcessFactorizedNumber.FactorizedResultscallback
//whenthecallcompletes.
publicvoidFactorizeNumber1()
{
//ThefollowingistheClientcode.
PrimeFactorizerpf=newPrimeFactorizer();
FactorizingCallbackfd=newFactorizingCallback(pf.Factorize);
//AsynchronousVariation1
intfactorizableNum=1000589023,temp=0;
//Createaninstanceoftheclasswhichisgoing
//tobecalledwhenthecallcompletes.
ProcessFactorizedNumberfc=newProcessFactorizedNumber(factorizableNum);
//DefinetheAsyncCallbackdelegate.
AsyncCallbackcb=newAsyncCallback(fc.FactorizedResults);
//Youcanstuffanyobjectasthestateobject.
Objectstate=newObject();
//AsynchronouslyinvoketheFactorizemethodonpf.
IAsyncResultar=fd.BeginInvoke(
factorizableNum,
reftemp,
reftemp,
cb,
state);
//
//Dosomeotherusefulwork.
//...
}
AsynchronousVariation2
//AsynchronousVariation2
//Waitsfortheresult.
publicvoidFactorizeNumber2()
{
//ThefollowingistheClientcode.
PrimeFactorizerpf=newPrimeFactorizer();
FactorizingCallbackfd=newFactorizingCallback(pf.Factorize);
//AsynchronousVariation1
intfactorizableNum=1000589023,temp=0;
//Createaninstanceoftheclasswhichisgoing
//tocalledwhenthecallcompletes.
ProcessFactorizedNumberfc=newProcessFactorizedNumber(factorizableNum);
//DefinetheAsyncCallbackdelegate.
AsyncCallbackcb=
newAsyncCallback(fc.FactorizedResults);
//Canstuffanyobjectasthestateobject.
Objectstate=newObject();
//AsynchronouslyinvoketheFactorizemethodonpf.
IAsyncResultar=fd.BeginInvoke(
factorizableNum,
reftemp,
reftemp,
null,
null);
ar.AsyncWaitHandle.WaitOne(10000,false);
if(ar.IsCompleted)
{
intfactor1=0,factor2=0;
//Obtaintheresult.
fd.EndInvoke(reffactor1,reffactor2,ar);
//Outputresults.
Console.WriteLine("Sequencial:Factorsof{0}:{1}{2}",
factorizableNum,factor1,factor2);
}
}

编译器和公共语言运行库支持

当编译器发出FactorizingCallback委托类时(在按如下所示分析其定义后),它将使用异步方法签名以及Invoke方法生成BeginInvokeEndInvoke方法,如以下代码中所示:
publicclassFactorizingCallback:delegate
{
publicboolInvoke(
intfactorizableNum,
refintprimefactor1,
refintprimefactor2);
//Thefollowingcodewassuppliedbythecompiler.
publicIAsyncResultBeginInvoke(
intfactorizableNum,
refintprimefactor1,
refintprimefactor2,
AsyncCallbackcb,
ObjectAsyncState
);
//Thefollowingcodewassuppliedbythecompiler.
publicboolEndInvoke(
refintprimefactor1,
refintprimefactor2,
IAsyncResultar);
}

编译器提供的委托BeginInvoke和EndInvoke方法

使用用户指定的委托签名,编译器应发出具有InvokeBeginInvokeEndInvoke方法的委托类。BeginInvokeEndInvoke方法应被修饰为本机的。因为这些方法被标记为本机的,所以公共语言运行库在类加载时自动提供该实现。加载程序确保它们未被重写。

异步委托编程示例

以下代码通过求解某些数字因子的简单类阐释如何使用.NET异步编程。
usingSystem;
usingSystem.Threading;
usingSystem.Runtime.Remoting;
//Createanasynchronousdelegate.
publicdelegateboolFactorizingCallback(
intfactorizableNum,
refintprimefactor1,
refintprimefactor2);
//Createaclassthefactorizersthenumber.
publicclassPrimeFactorizer.
{
publicboolFactorize(
intfactorizableNum,
refintprimefactor1,
refintprimefactor2)
{
primefactor1=1;
primefactor2=factorizableNum;
//Factorizeusingalow-techapproach.
for(inti=2;i<factorizableNum;i++)
{
if(0==(factorizableNum%i))
{
primefactor1=i;
primefactor2=factorizableNum/i;
break;
}
}
if(1==primefactor1)
returnfalse;
else
returntrue;
}
}
//Classthatreceivesacallbackwhenthetheresultsareavailable.
publicclassProcessFactorizedNumber
{
privateint_ulNumber;
publicProcessFactorizedNumber(intnumber)
{
_ulNumber=number;
}
//Notethequalifierisone-way.
[OneWayAttribute()]
publicvoidFactorizedResults(IAsyncResultar)
{
intfactor1=0,factor2=0;
//ExtractthedelegatefromtheAsyncResult.
FactorizingCallbackfd=(FactorizingCallback)((AsyncResult)ar).AsyncDelegate;
//Obtaintheresult.
fd.EndInvoke(reffactor1,reffactor2,ar);
//Outputresults.
Console.WriteLine("OnCallBack:Factorsof{0}:{1}{2}",
_ulNumber,factor1,factor2);
}
}
//ClassthatshowsvariationsofusingAsynchronous
publicclassSimple.
{
//ThefollowingdemonstratestheAsynchronousPatternusingacallback.
publicvoidFactorizeNumber1()
{
//Thefollowingistheclientcode.
PrimeFactorizerpf=newPrimeFactorizer();
FactorizingCallbackfd=newFactorizingCallback(pf.Factorize);
intfactorizableNum=1000589023,temp=0;
//Createaninstanceoftheclasswhichisgoing
//tobecalledwhenthecallcompletes.
ProcessFactorizedNumberfc=newProcessFactorizedNumber(factorizableNum);
//DefinetheAsyncCallbackdelegate.
AsyncCallbackcb=newAsyncCallback(fc.FactorizedResults);
//Youcanstuffanyobjectasthestateobject
Objectstate=newObject();
//AsynchronouslyinvoketheFactorizemethodonpf.
IAsyncResultar=fd.BeginInvoke(
factorizableNum,
reftemp,
reftemp,
cb,
state);
//
//Dosomeotherusefulwork.
//...
}
//ThefollowingdemonstratestheAsynchronousPatternusingaBeginInvoke,followedbywaitingwithatimeout.
publicvoidFactorizeNumber2()
{
//Thefollowingistheclientcode.
PrimeFactorizerpf=newPrimeFactorizer();
FactorizingCallbackfd=newFactorizingCallback(pf.Factorize);
intfactorizableNum=1000589023,temp=0;
//Createaninstanceoftheclasswhichisgoing
//tobecalledwhenthecallcompletes.
ProcessFactorizedNumberfc=newProcessFactorizedNumber(factorizableNum);
//DefinetheAsyncCallbackdelegate.
AsyncCallbackcb=
newAsyncCallback(fc.FactorizedResults);
//Youcanstuffanyobjectasthestateobject.
Objectstate=newObject();
//AsynchronouslyinvoketheFactorizemethodonpf.
IAsyncResultar=fd.BeginInvoke(
factorizableNum,
reftemp,
reftemp,
null,
null);
ar.AsyncWaitHandle.WaitOne(10000,false);
if(ar.IsCompleted)
{
intfactor1=0,factor2=0;
//Obtaintheresult.
fd.EndInvoke(reffactor1,reffactor2,ar);
//Outputresults.
Console.WriteLine("Sequencial:Factorsof{0}:{1}{2}",
factorizableNum,factor1,factor2);
}
}
publicstaticvoidMain(String[]args)
{
Simplesimple=newSimple();
simple.FactorizeNumber1();
simple.FactorizeNumber2();
}
}

总结

上面是VS.NET中.NET异步编程方面的基本概念和示例代码,整理出来给大家参考一下。有任何建议请MAIL我paulni@citiz.net。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: