分享
 
 
 

使用.NET 异步编程(2)

王朝c#·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

异步委托

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

使用委托

对于异步编程,如果调用方使用一个委托,则该调用方在调用一个方法时必须定义该委托。在以下代码示例中,首先定义该委托,接着创建该委托的实例,然后调用它。下面的示例说明为异步调用 Factorize 方法定义一个模式的调用方:

using System;

using System.Runtime.Remoting;

public delegate bool FactorizingCallback(

int factorizableNum,

ref int primefactor1,

ref int primefactor2);

// This is a class that receives a callback when the the results are available.

public class ProcessFactorizedNumber

{

private int _ulNumber;

public ProcessFactorizedNumber(int number)

{

_ulNumber = number;

}

// Note the qualifier one-way.

[OneWayAttribute()]

public void FactorizedResults(IAsyncResult ar)

{

int factor1=0, factor2=0;

// Extract the delegate from the AsyncResult.

FactorizingCallback fd =

(FactorizingCallback) ((AsyncResult)ar).AsyncDelegate;

// Obtain the result.

fd.EndInvoke(ref factor1, ref factor2, ar);

// Output results.

Console.WriteLine("On CallBack: Factors of {0} : {1} {2}",

_ulNumber, factor1, factor2);

}

}

Asynchronous Variation 1 – call

// The Asynchronous Variation 1 call, calls

// the ProcessFactorizedNumber.FactorizedResults callback

// when the call completes.

public void FactorizeNumber1()

{

// The following is the Client code.

PrimeFactorizer pf = new PrimeFactorizer();

FactorizingCallback fd = new FactorizingCallback(pf.Factorize);

// Asynchronous Variation 1

int factorizableNum = 1000589023, temp=0;

// Create an instance of the class which is going

// to be called when the call completes.

ProcessFactorizedNumber fc = new ProcessFactorizedNumber(factorizableNum);

// Define the AsyncCallback delegate.

AsyncCallback cb = new AsyncCallback(fc.FactorizedResults);

// You can stuff any object as the state object.

Object state = new Object();

// Asynchronously invoke the Factorize method on pf.

IAsyncResult ar = fd.BeginInvoke(

factorizableNum,

ref temp,

ref temp,

cb,

state);

//

// Do some other useful work.

//. . .

}

Asynchronous Variation 2

// Asynchronous Variation 2

// Waits for the result.

public void FactorizeNumber2()

{

// The following is the Client code.

PrimeFactorizer pf = new PrimeFactorizer();

FactorizingCallback fd = new FactorizingCallback(pf.Factorize);

// Asynchronous Variation 1

int factorizableNum = 1000589023, temp=0;

// Create an instance of the class which is going

// to called when the call completes.

ProcessFactorizedNumber fc = new ProcessFactorizedNumber(factorizableNum);

// Define the AsyncCallback delegate.

AsyncCallback cb =

new AsyncCallback(fc.FactorizedResults);

// Can stuff any object as the state object.

Object state = new Object();

// Asynchronously invoke the Factorize method on pf.

IAsyncResult ar = fd.BeginInvoke(

factorizableNum,

ref temp,

ref temp,

null,

null);

ar.AsyncWaitHandle.WaitOne(10000, false);

if (ar.IsCompleted)

{

int factor1=0, factor2=0;

// Obtain the result.

fd.EndInvoke(ref factor1, ref factor2, ar);

// Output results.

Console.WriteLine("Sequencial : Factors of {0} : {1} {2}",

factorizableNum, factor1, factor2);

}

}

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

当编译器发出 FactorizingCallback 委托类时(在按如下所示分析其定义后),它将使用异步方法签名以及 Invoke 方法生成 BeginInvoke 和 EndInvoke 方法,如以下代码中所示:

public class FactorizingCallback : delegate

{

public bool Invoke(

int factorizableNum,

ref int primefactor1,

ref int primefactor2);

// The following code was supplied by the compiler.

public IAsyncResult BeginInvoke(

int factorizableNum,

ref int primefactor1,

ref int primefactor2,

AsyncCallback cb,

Object AsyncState

);

// The following code was supplied by the compiler.

public bool EndInvoke(

ref int primefactor1,

ref int primefactor2,

IAsyncResult ar);

}

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

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

异步委托编程示例

以下代码通过求解某些数字因子的简单类阐释如何使用 .NET 异步编程。

using System;

using System.Threading;

using System.Runtime.Remoting;

// Create an asynchronous delegate.

public delegate bool FactorizingCallback(

int factorizableNum,

ref int primefactor1,

ref int primefactor2);

// Create a class the factorizers the number.

public class PrimeFactorizer.

{

public bool Factorize(

int factorizableNum,

ref int primefactor1,

ref int primefactor2)

{

primefactor1 = 1;

primefactor2 = factorizableNum;

// Factorize using a low-tech approach.

for (int i=2;i<factorizableNum;i++)

{

if (0 == (factorizableNum % i))

{

primefactor1 = i;

primefactor2 = factorizableNum / i;

break;

}

}

if (1 == primefactor1 )

return false;

else

return true ;

}

}

// Class that receives a callback when the the results are available.

public class ProcessFactorizedNumber

{

private int _ulNumber;

public ProcessFactorizedNumber(int number)

{

_ulNumber = number;

}

// Note the qualifier is one-way.

[OneWayAttribute()]

public void FactorizedResults(IAsyncResult ar)

{

int factor1=0, factor2=0;

// Extract the delegate from the AsyncResult.

FactorizingCallback fd = (FactorizingCallback)((AsyncResult)ar).AsyncDelegate;

// Obtain the result.

fd.EndInvoke(ref factor1, ref factor2, ar);

// Output results.

Console.WriteLine("On CallBack: Factors of {0} : {1} {2}",

_ulNumber, factor1, factor2);

}

}

// Class that shows variations of using Asynchronous

public class Simple.

{

// The following demonstrates the Asynchronous Pattern using a callback.

public void FactorizeNumber1()

{

// The following is the client code.

PrimeFactorizer pf = new PrimeFactorizer();

FactorizingCallback fd = new FactorizingCallback(pf.Factorize);

int factorizableNum = 1000589023, temp=0;

// Create an instance of the class which is going

// to be called when the call completes.

ProcessFactorizedNumber fc = new ProcessFactorizedNumber(factorizableNum);

// Define the AsyncCallback delegate.

AsyncCallback cb = new AsyncCallback(fc.FactorizedResults);

// You can stuff any object as the state object

Object state = new Object();

// Asynchronously invoke the Factorize method on pf.

IAsyncResult ar = fd.BeginInvoke(

factorizableNum,

ref temp,

ref temp,

cb,

state);

//

// Do some other useful work.

//. . .

}

// The following demonstrates the Asynchronous Pattern using a BeginInvoke, followed by waiting with a timeout.

public void FactorizeNumber2()

{

// The following is the client code.

PrimeFactorizer pf = new PrimeFactorizer();

FactorizingCallback fd = new FactorizingCallback(pf.Factorize);

int factorizableNum = 1000589023, temp=0;

// Create an instance of the class which is going

// to be called when the call completes.

ProcessFactorizedNumber fc = new ProcessFactorizedNumber(factorizableNum);

// Define the AsyncCallback delegate.

AsyncCallback cb =

new AsyncCallback(fc.FactorizedResults);

// You can stuff any object as the state object.

Object state = new Object();

// Asynchronously invoke the Factorize method on pf.

IAsyncResult ar = fd.BeginInvoke(

factorizableNum,

ref temp,

ref temp,

null,

null);

ar.AsyncWaitHandle.WaitOne(10000, false);

if (ar.IsCompleted)

{

int factor1=0, factor2=0;

// Obtain the result.

fd.EndInvoke(ref factor1, ref factor2, ar);

// Output results.

Console.WriteLine("Sequencial : Factors of {0} : {1} {2}",

factorizableNum, factor1, factor2);

}

}

public static void Main(String[] args)

{

Simple simple = new Simple();

simple.FactorizeNumber1();

simple.FactorizeNumber2();

}

}

总结

上面是VS.NET中.NET异步编程方面的基本概念和示例代码,整理出来给大家参考一下。有任何建议请MAIL我 paulni@citiz.net

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有