如何使用.NET异步编程?

如何使用.NET异步编程?

异步委托

异步委托提供以异步方式调用同步方法的能力。当同步调用一个委托时,调用方法直接对当前线程调用目标方法。如果编译器支持异步委托,则它将生成该调用方法以及 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 方法生成 BeginInvokeEndInvoke 方法,如以下代码中所示:

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 方法

使用用户指定的委托签名,编译器应发出具有 InvokeBeginInvoke 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