使用委託實現同步回調與異步回調

使用委託能夠執行的一項有用操做是實現回調。回調是傳入函數的方法,在函數結束執行時調用該方法。

例如,有一個執行一系列數學操做的函數。在調用該函數時,也向其傳遞一個回調方法,從而在函數完成其計算工做時,調用回調方法,向用戶通知計算結果。

 

同步回調

   首先聲明兩個方法:css

AddTwoNumbers():接受兩個整型實參以及一個類型委託html

ResultCallback():接受一個字符串,並顯示出來。代碼以下: 異步

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DelegateCallBack
{
    class Program
    {
        delegate void CallbackDelegate(string msg);

        static void Main(string[] args)
        {
            //方式一:
            CallbackDelegate result = ResultCallback;
            AddTwoNumbers(5, 3, result);

            //方式二:
            AddTwoNumbers(5, 3, ResultCallback);

            Console.Read();
        }

       

        static private void AddTwoNumbers(int num1,int num2,CallbackDelegate callback)
        {
            int result = num1 + num2;

            callback(result.ToString());
        }

        static private void ResultCallback(string msg)
        {
            Console.WriteLine(msg);
        }
    }
}

異步回調函數

回調在異步狀況下最有用。前面實例中說明的回調是同步回調,也就是按順序調用函數。若是AddTwoNumbers方法花費較長時間來執行,則該函數以後的全部的語句將被阻塞。spa

組織較好的方式是異步調用AddTwoNumbers方法。異步調用函數容許主程序繼續執行,而不須要等待該函數返回。線程

在這種異步模型中,當調用AddTwoNumbers函數時,在其後的語句繼續執行。當函數結束時,他調用ResultCallback函數。3d

下面使用異步回調重寫前面的程序:code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading.Tasks;

namespace DelegateCallBack
{
    class Program
    {
        delegate int MethodDelegate(int num1, int num2);

        static void Main(string[] args)
        {
           
            MethodDelegate result = AddTwoNumbers;

            //引用異步操做完成時調用的方法
            AsyncCallback callback = new AsyncCallback(ResultCallback);

            Console.WriteLine("開始異步調用");

            IAsyncResult iresult = result.BeginInvoke(5, 3, callback, null);

            Console.WriteLine("主程序繼續執行");

   
            Console.Read();
        }

       

        static private int AddTwoNumbers(int num1,int num2)
        {
            int result = num1 + num2;

            System.Threading.Thread.Sleep(5000);

            return result;
        }

        static private void ResultCallback(IAsyncResult ar)
        {
            MethodDelegate dele = (MethodDelegate)((AsyncResult)ar).AsyncDelegate;

            int result = dele.EndInvoke(ar);

            Console.WriteLine("result={0}",result);
        }
    }
}

程序一運行:htm

360軟件小助手截圖20130810134151

五秒後blog

360軟件小助手截圖20130810134234

如今咱們分析下程序,首先咱們定義一個委託類型,從而能夠指向AddTwoNumbers方法。

接下來,定義一個類型爲AsyncCallback的委託。AsyncCallback是引用某個方法的委託,當異步操做完成時調用該方法。

使用result 委託的BeginInvoke()方法異步調用AddTwoNumbers(),而且向該方法傳遞兩個整型以及在該方法結束執行時回調的委託。

BeginInvoke()方法異步調用委託,在調用異步委託以後,下一條語句會繼續執行。該方法返回類型爲IAsyncResult 的變量,該變量表示異步操做的狀態。

在ResultCallback方法中,首先使用AsyncDelegate特性得到指向AddTwoNumbers()方法的委託,該特性返回進行異步調用的委託。接下來,使用EndInvoke()方法會的異步調用的結果,向該方法傳遞IAsycResult變量。

在使用異步回調時,能夠經過在不一樣的線程中執行程序的不一樣部分來使程序更快速的響應。

相關文章
相關標籤/搜索