C# 多线程,异步,并行编程

(一) 同步调用

委托的Invoke方法用来进行同步调用。同步调用也可以叫阻塞调用,它将阻塞当前线程,然后执行调用,调用完毕后再继续向下进行。

public delegate int AddHandler(int a, int b);
public class 加法类
{
    public static int Add(int a, int b)
    {
        Console.WriteLine("开始计算:" + a + "+" + b);
        Thread.Sleep(3000); //模拟该方法运行三秒
        Console.WriteLine("计算完成!");
        return a + b;
    }
}
调用示例:
Console.WriteLine("===== 同步调用 SyncInvokeTest =====");
AddHandler handler = new AddHandler(加法类.Add);
int result = handler.Invoke(1, 2);

Console.WriteLine("继续做别的事情。。。");
Console.WriteLine(result);
Console.ReadKey();
image

(二) 异步调用

异步调用不阻塞线程,而是把调用塞到线程池中,程序主线程或UI线程可以继续执行。委托的异步调用通过BeginInvoke和EndInvoke来实现。

下图看出异步调用没有阻塞主线程Main()的执行。

Console.WriteLine("===== 异步调用 AsyncInvokeTest =====");
AddHandler handler = new AddHandler(加法类.Add);
//IAsyncResult: 异步操作接口(interface)
//BeginInvoke: 委托(delegate)的一个异步方法的开始
IAsyncResult result = handler.BeginInvoke(1, 2, null, null);
Console.WriteLine("继续做别的事情。。。");
//异步操作返回
Console.WriteLine(handler.EndInvoke(result));
Console.ReadKey();
image

(三)异步回调

用回调函数,当调用结束时会自动调用回调函数,可以在回调函数里触发EndInvoke,这样就可以避免程序一直占用一个线程,就释放掉了线程

static void Main3() { Console.WriteLine("===== 异步回调 AsyncInvokeTest ====="); AddHandler handler = new AddHandler(加法类.Add); //异步操作接口(注意BeginInvoke方法的不同!) IAsyncResult result = handler.BeginInvoke(1, 2, new AsyncCallback(回调函数), "AsycState:OK,这里输出的是AsyncState的参数内容"); Console.WriteLine("继续做别的事情。。。"); Console.ReadKey(); } static void 回调函数(IAsyncResult result) { //result 是“加法类.Add()方法”的返回值 //AsyncResult 是IAsyncResult接口的一个实现类,引用空间:System.Runtime.Remoting.Messaging //AsyncDelegate 属性可以强制转换为用户定义的委托的实际类。 AddHandler handler = (AddHandler)((AsyncResult)result).AsyncDelegate; Console.WriteLine(handler.EndInvoke(result)); Console.WriteLine(result.AsyncState); }

image

http://www.wischik.com/lu/AsyncSilverlight/AsyncSamples.html

 

posted @ 2015-11-01 00:00  李华丽  阅读(482)  评论(0编辑  收藏  举报
AmazingCounters.com