C#非同步方法呼叫執行

來源:互聯網
上載者:User

標籤:

C#非同步方法呼叫執行轉於http://blog.csdn.net/wanlong360599336/article/details/8781477

using System.Collections.Generic;  using System.Linq;  using System.Text;  using System.Threading;  using System.Runtime.Remoting.Messaging;    namespace ConsoleApplication1  {      public delegate int AddHandler(int a, int b);      public class AddMethod      {          public static int Add(int a, int b)          {              Console.WriteLine("開始計算:" + a + "+" + b);              Thread.Sleep(3000); //類比該方法運行三秒              Console.WriteLine("計算完成!");              return a + b;          }      }                      //**************同步調用***********      //委託的Invoke方法用來進行同步調用。同步調用也可以叫阻塞調用,它將阻塞當前線程,然後執行調用,調用完畢後再繼續向下進行。        //**************非同步呼叫***********      //非同步呼叫不阻塞線程,而是把調用塞到線程池中,程式主線程或UI線程可以繼續執行。      //委託的非同步呼叫通過BeginInvoke和EndInvoke來實現。        //**************非同步回調***********      //用回呼函數,當調用結束時會自動調用回呼函數,解決了為等待調用結果,而讓線程依舊被阻塞的局面。        //注意: BeginInvoke和EndInvoke必須成對調用.即使不需要傳回值,但EndInvoke還是必須調用,否則可能會造成記憶體流失。      class Program      {          static void Main(string[] args)          {              Console.WriteLine("===== 同步調用 SyncInvokeTest =====");              AddHandler handler = new AddHandler(AddMethod.Add);              int result=handler.Invoke(1,2);              Console.WriteLine("繼續做別的事情。。。");                Console.WriteLine(result);              Console.ReadKey();                  Console.WriteLine("===== 非同步呼叫 AsyncInvokeTest =====");              AddHandler handler1 = new AddHandler(AddMethod.Add);              IAsyncResult result1=handler1.BeginInvoke(1,2,null,null);              Console.WriteLine("繼續做別的事情。。。");                //非同步作業返回              Console.WriteLine(handler1.EndInvoke(result1));              Console.ReadKey();                Console.WriteLine("===== 非同步回調 AsyncInvokeTest =====");              AddHandler handler2 = new AddHandler(AddMethod.Add);              IAsyncResult result2 = handler2.BeginInvoke(1, 2, new AsyncCallback(Callback), null);              Console.WriteLine("繼續做別的事情。。。");              Console.ReadKey();                  //非同步委託,也可以參考如下寫法:              //Action<object> action=(obj)=>method(obj);              //action.BeginInvoke(obj,ar=>action.EndInvoke(ar),null);              //簡簡單單兩句話就可以完成一部操作。          }          static void Callback(IAsyncResult result)          {              AddHandler handler = (AddHandler)((AsyncResult)result).AsyncDelegate;              Console.WriteLine(handler.EndInvoke(result));          }      }  }






C#非同步方法呼叫執行

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.