可以这样理解:
委托是一个函数的指针,但它可以被异步地去调用。
当我们声明一个委托的时候,例如:
public delegate bool FactorizingAsyncDelegate(
int factorizableNum,
ref int primefactor1,
ref int primefactor2);系统是这样解释的:public class FactorizingAsyncDelegate : 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);
}可以理解为这段代码是系统自己生成的这里的Invoke方法是同步调用,也就是我们一般用的Event和在声明类里对委托的直接调用。
但它的BeginInvoke和EndInvoke却是异步的。即当我们使用委托的异步的BeginInvoke和EndInvoke时,本线程是post一个Message出去的,这个Message会启动一个新的线程去执行委托对应的函数(方法),至于你说的回调,就是在这个函数(方法)执行完毕后的一个通知,但这个通知也是通过委托来完成的,它又可以执行你回调的委托对应的函数(方法)。至于例子,虽然你不喜欢看MSDN
但这个例子却是MSDN上的:[C#]
using System;
using System.Threading;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Messaging;// Create an asynchronous delegate.
public delegate bool FactorizingAsyncDelegate (
int factorizableNum,
ref int primefactor1,
ref int primefactor2);// Create a class that 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 results are available.
public class ProcessFactorizedNumber
{
private int _ulNumber; public ProcessFactorizedNumber(int number)
{
_ulNumber = number;
} // Note that the qualifier is one-way.
[OneWayAttribute()]
public void FactorizedResults(IAsyncResult ar)
{
int factor1=0, factor2=0; // Extract the delegate from the AsyncResult.
FactorizingAsyncDelegate fd = (FactorizingAsyncDelegate)((AsyncResult)ar).AsyncDelegate; // Obtain the result.
fd.EndInvoke(ref factor1, ref factor2, ar); // Output the 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();
FactorizingAsyncDelegate fd = new FactorizingAsyncDelegate (pf.Factorize); int factorizableNum = 1000589023, temp=0; // Create an instance of the class that 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 use 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 time-out.
public void FactorizeNumber2()
{
// The following is the client code.
PrimeFactorizer pf = new PrimeFactorizer();
FactorizingAsyncDelegate fd = new FactorizingAsyncDelegate (pf.Factorize); int factorizableNum = 1000589023, temp=0; // Create an instance of the class that 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 use 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 the results. Console.WriteLine("Sequential : Factors of {0} : {1} {2}",
factorizableNum, factor1, factor2); }
}
public static void Main(String[] args)
{
Simple simple = new Simple();
simple.FactorizeNumber1();
simple.FactorizeNumber2();
}
}
委托是一个函数的指针,但它可以被异步地去调用。
当我们声明一个委托的时候,例如:
public delegate bool FactorizingAsyncDelegate(
int factorizableNum,
ref int primefactor1,
ref int primefactor2);系统是这样解释的:public class FactorizingAsyncDelegate : 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);
}可以理解为这段代码是系统自己生成的这里的Invoke方法是同步调用,也就是我们一般用的Event和在声明类里对委托的直接调用。
但它的BeginInvoke和EndInvoke却是异步的。即当我们使用委托的异步的BeginInvoke和EndInvoke时,本线程是post一个Message出去的,这个Message会启动一个新的线程去执行委托对应的函数(方法),至于你说的回调,就是在这个函数(方法)执行完毕后的一个通知,但这个通知也是通过委托来完成的,它又可以执行你回调的委托对应的函数(方法)。至于例子,虽然你不喜欢看MSDN
但这个例子却是MSDN上的:[C#]
using System;
using System.Threading;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Messaging;// Create an asynchronous delegate.
public delegate bool FactorizingAsyncDelegate (
int factorizableNum,
ref int primefactor1,
ref int primefactor2);// Create a class that 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 results are available.
public class ProcessFactorizedNumber
{
private int _ulNumber; public ProcessFactorizedNumber(int number)
{
_ulNumber = number;
} // Note that the qualifier is one-way.
[OneWayAttribute()]
public void FactorizedResults(IAsyncResult ar)
{
int factor1=0, factor2=0; // Extract the delegate from the AsyncResult.
FactorizingAsyncDelegate fd = (FactorizingAsyncDelegate)((AsyncResult)ar).AsyncDelegate; // Obtain the result.
fd.EndInvoke(ref factor1, ref factor2, ar); // Output the 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();
FactorizingAsyncDelegate fd = new FactorizingAsyncDelegate (pf.Factorize); int factorizableNum = 1000589023, temp=0; // Create an instance of the class that 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 use 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 time-out.
public void FactorizeNumber2()
{
// The following is the client code.
PrimeFactorizer pf = new PrimeFactorizer();
FactorizingAsyncDelegate fd = new FactorizingAsyncDelegate (pf.Factorize); int factorizableNum = 1000589023, temp=0; // Create an instance of the class that 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 use 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 the results. Console.WriteLine("Sequential : Factors of {0} : {1} {2}",
factorizableNum, factor1, factor2); }
}
public static void Main(String[] args)
{
Simple simple = new Simple();
simple.FactorizeNumber1();
simple.FactorizeNumber2();
}
}
http://community.csdn.net/Expert/topic/3649/3649200.xml?temp=.330456
这是一个例子加问题!