基於委託的非同步

來源:互聯網
上載者:User
先講一下C#的同步調用,例子很簡單,一看就明白,通過線程號就可以看出,是同步執行的    public delegate double AddEventHandler(int a, int b);    class AddCalss    {        public static double Add(int a, int b)        {            Console.WriteLine("我在AddClass中的Add方法下,線程是:"+Thread.CurrentThread.ManagedThreadId);            return a + b;        }    }    class Test    {        static void Main()        {            Console.WriteLine("我在Main方法中,這是方法的第一行!線程是:"+Thread.CurrentThread.ManagedThreadId);            AddEventHandler add = new AddEventHandler(AddCalss.Add);            //invoke的傳回值就是自訂方法的傳回值            double result = add.Invoke(1, 1);            Console.WriteLine("計算結束......");            Console.WriteLine("返回的結果是" + result);            Console.ReadKey();        }    }接下來解釋兩個對象:AsyncCallback,IAsyncResultAsyncCallback委託,概括來說它的作用是在一個單獨的線程中處理非同步作業的結果。AsyncCallback就是一個非同步操作完成後要執行的回呼函數,該函數把IAsyncResult作為它的參數,由這個參數得到相應的非同步作業的結果IAsyncResult介面,就像是一個信使,作為一個參數,把有用的資訊帶給非同步線程看一下下面的代碼,再根據代碼做詳細的分析:   //定義一個委託,因為這是基於委託的非同步呼叫    public delegate int AddEventHandler(int a, int b);    class AddCalss    {        //稍後該方法會用作對一個委託的註冊        public static int Add(int a, int b)        {            Console.WriteLine("我在AddClass中的Add方法下,線程號:" + Thread.CurrentThread.ManagedThreadId);            return a + b;        }    }    class Test    {        static AsyncCallback MyCall;        static void Main()        {            //委託的註冊            MyCall = Method1;            Console.WriteLine("我在Main方法中,這是方法的第一行!,線程號是:" + Thread.CurrentThread.ManagedThreadId);            //註冊自己聲明的委託            AddEventHandler add = new AddEventHandler(AddCalss.Add);            //用BeginInvoke開始非同步作業,一步的執行AddClass.Add方法,前面兩個是要傳入該方法的參數,            //第三個是AsyncCallback委託,第四個是傳一個附加對象,會由AsyncState得到。            IAsyncResult result = add.BeginInvoke(1, 2, MyCall, "這是傳過來的一句話");            Console.WriteLine("計算完畢.......");            Console.ReadKey();        }        //實現AsyncCallback委託的方法,當然該類型跟該委託完全一樣,會用此方法註冊AsyncCallback委託        //上面的Add方法是一個非同步操作,而此操作的結果都返回到下面這個方法中,在裡面再進行你想要的操作。        public static void Method1(IAsyncResult result)        {               //該類實現了IAsyncResult介面,豐富了許多的方法。            AsyncResult aResult = (AsyncResult)result;            //在新線程中得到原委託的對象。            AddEventHandler handler = (AddEventHandler)aResult.AsyncDelegate;            //EndInvoke返回的就是Add方法執行後的結果。            Console.WriteLine(handler.EndInvoke(result));            Console.WriteLine(result.AsyncState);            Console.WriteLine("我在Method1方法中,線程號:" + Thread.CurrentThread.ManagedThreadId);        }    }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.