异步方法

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. /* 
  6.  * 同步方法调用在程序继续执行之前需要等待同步方法执行完毕返回结果 
  7.  * 异步方法则在被调用之后立即返回以便程序在被调用方法完成其任务的同时执行其它操作 
  8.  * Tomcat by 2011-08-08 17:39 
  9.  */  
  10. namespace ThreadConsoleApplication  
  11. {  
  12.     class Program  
  13.     {  
  14.         [STAThread]  
  15.         static void Main(string[] args)  
  16.         {  
  17.             long start = 0;  
  18.             long end = 0;  
  19.             Program c = new Program();  
  20.             start = DateTime.Now.Ticks;  
  21.             Console.BufferHeight = 507; //控制控制台的行数  
  22.             //异步方法一 自己调用EndInvoke方法  
  23.             //异步执行方法的最简单方式是以BeginInvoke开始,对主线程执行一些操作,然后调用EndInvoke,EndInvoke直到异步调用完成后才返回  
  24.   
  25.             //AsyncEventHandler asy = new AsyncEventHandler(c.Event1);  
  26.             //IAsyncResult ia = asy.BeginInvoke(null, null);  
  27.             //c.Event2();  
  28.             //asy.EndInvoke(ia);  
  29.   
  30.             //异步方法二 采用查询(IsComplated 属性)  
  31.             //IAsyncResult.IsCompleted属性获取异步操作是否已完成的指示,发现异步调用何时完成.  
  32.   
  33.             //AsyncEventHandler asy = new AsyncEventHandler(c.Event1);  
  34.             //IAsyncResult ia = asy.BeginInvoke(null, null);  
  35.             //c.Event2();  
  36.             //while (!ia.IsCompleted)  
  37.             //{  
  38.             //    Console.WriteLine("完成!!!!");  
  39.             //}  
  40.             //asy.EndInvoke(ia);  
  41.   
  42.             //异步方法三 采用AsyncWaitHandle来等待方法调用的完成  
  43.             //采用AsyncWaitHandle来等待方法调用的完成  
  44.   
  45.             //AsyncEventHandler asy = new AsyncEventHandler(c.Event1);  
  46.             //IAsyncResult ia = asy.BeginInvoke(null, null);  
  47.             //c.Event2();  
  48.             //ia.AsyncWaitHandle.WaitOne();  
  49.   
  50.             //异步方法四 利用回调函数  
  51.             //如果启动异步调用的线程不需要处理调用结果,则可以在调用完成时执行回调方法  
  52.             //要使用回调方法,必须将代表该方法的AsyncCallback委托传递给BeginInvoke  
  53.   
  54.             AsyncEventHandler asy = new AsyncEventHandler(c.Event1);  
  55.             asy.BeginInvoke(new AsyncCallback(c.CallbackMethod), asy);  
  56.             c.Event2();  
  57.   
  58.   
  59.             //同步调用  
  60.             //c.Event1();  
  61.             //c.Event2();  
  62.   
  63.             end = DateTime.Now.Ticks;  
  64.             Console.WriteLine("时间刻度差=" + Convert.ToString(end - start));  
  65.             Console.ReadLine();  
  66.         }  
  67.   
  68.         public delegate void AsyncEventHandler();  
  69.   
  70.         void Event1()  
  71.         {  
  72.             Console.WriteLine("Event1 Start");  
  73.             System.Threading.Thread.Sleep(20000);  
  74.             Console.WriteLine("Event1 End");  
  75.         }  
  76.         void Event2()  
  77.         {  
  78.             Console.WriteLine("Event2 Start");  
  79.             int i = 1;  
  80.             while (i < 500)  
  81.             {  
  82.                 i = i + 1;  
  83.                 Console.WriteLine("Event2" + i.ToString());  
  84.             }  
  85.             Console.WriteLine("Event2 End");  
  86.         }  
  87.         void CallbackMethod(IAsyncResult ar)  //监视异步调用结果  
  88.         {  
  89.             ((AsyncEventHandler)ar.AsyncState).EndInvoke(ar);  
  90.         }  
  91.     }  

原文地址:https://www.cnblogs.com/vebest/p/2183844.html