参考文章 原文
异步调用可以避免主线程受工作线程阻塞,即工作线程执行的过程中,主线程依然可以往下运行,不必等待工作线程完成。下面是一个简单的异步调用加法函数的例子。
using System;
using System.Threading;
// the namespace for AsyncResult.
using System.Runtime.Remoting.Messaging;
namespace AsyncCallback
{
// Delegate for add method.
public delegate int TwoOperands(int a,int b);
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Main is running on thread " + Thread.CurrentThread.ManagedThreadId);
TwoOperands operation = new TwoOperands(Add);
//async calling the add.
//new System.AsyncCallback(CallbackHandler) 回调处理函数, 异步调用结束后,runtime会调用该函数。
// "Async parameter" 作为回调参数,传给回调函数,通过 AsyncResult.AsyncState 获取
operation.BeginInvoke(2, 3, new System.AsyncCallback(Callbac