- using System;  
 - using System.Collections.Generic;  
 - using System.Linq;  
 - using System.Text;  
 - /* 
 -  * 同步方法调用在程序继续执行之前需要等待同步方法执行完毕返回结果 
 -  * 异步方法则在被调用之后立即返回以便程序在被调用方法完成其任务的同时执行其它操作 
 -  * Tomcat by 2011-08-08 17:39 
 -  */  
 - namespace ThreadConsoleApplication  
 - {  
 -     class Program  
 -     {  
 -         [STAThread]  
 -         static void Main(string[] args)  
 -         {  
 -             long start = 0;  
 -             long end = 0;  
 -             Program c = new Program();  
 -             start = DateTime.Now.Ticks;  
 -             Console.BufferHeight = 507; //控制控制台的行数  
 -             //异步方法一 自己调用EndInvoke方法  
 -             //异步执行方法的最简单方式是以BeginInvoke开始,对主线程执行一些操作,然后调用EndInvoke,EndInvoke直到异步调用完成后才返回  
 -   
 -             //AsyncEventHandler asy = new AsyncEventHandler(c.Event1);  
 -             //IAsyncResult ia = asy.BeginInvoke(null, null);  
 -             //c.Event2();  
 -             //asy.EndInvoke(ia);  
 -   
 -             //异步方法二 采用查询(IsComplated 属性)  
 -             //IAsyncResult.IsCompleted属性获取异步操作是否已完成的指示,发现异步调用何时完成.  
 -   
 -             //AsyncEventHandler asy = new AsyncEventHandler(c.Event1);  
 -             //IAsyncResult ia = asy.BeginInvoke(null, null);  
 -             //c.Event2();  
 -             //while (!ia.IsCompleted)  
 -             //{  
 -             //    Console.WriteLine("完成!!!!");  
 -             //}  
 -             //asy.EndInvoke(ia);  
 -   
 -             //异步方法三 采用AsyncWaitHandle来等待方法调用的完成  
 -             //采用AsyncWaitHandle来等待方法调用的完成  
 -   
 -             //AsyncEventHandler asy = new AsyncEventHandler(c.Event1);  
 -             //IAsyncResult ia = asy.BeginInvoke(null, null);  
 -             //c.Event2();  
 -             //ia.AsyncWaitHandle.WaitOne();  
 -   
 -             //异步方法四 利用回调函数  
 -             //如果启动异步调用的线程不需要处理调用结果,则可以在调用完成时执行回调方法  
 -             //要使用回调方法,必须将代表该方法的AsyncCallback委托传递给BeginInvoke  
 -   
 -             AsyncEventHandler asy = new AsyncEventHandler(c.Event1);  
 -             asy.BeginInvoke(new AsyncCallback(c.CallbackMethod), asy);  
 -             c.Event2();  
 -   
 -   
 -             //同步调用  
 -             //c.Event1();  
 -             //c.Event2();  
 -   
 -             end = DateTime.Now.Ticks;  
 -             Console.WriteLine("时间刻度差=" + Convert.ToString(end - start));  
 -             Console.ReadLine();  
 -         }  
 -   
 -         public delegate void AsyncEventHandler();  
 -   
 -         void Event1()  
 -         {  
 -             Console.WriteLine("Event1 Start");  
 -             System.Threading.Thread.Sleep(20000);  
 -             Console.WriteLine("Event1 End");  
 -         }  
 -         void Event2()  
 -         {  
 -             Console.WriteLine("Event2 Start");  
 -             int i = 1;  
 -             while (i < 500)  
 -             {  
 -                 i = i + 1;  
 -                 Console.WriteLine("Event2" + i.ToString());  
 -             }  
 -             Console.WriteLine("Event2 End");  
 -         }  
 -         void CallbackMethod(IAsyncResult ar)  //监视异步调用结果  
 -         {  
 -             ((AsyncEventHandler)ar.AsyncState).EndInvoke(ar);  
 -         }  
 -     }  
 - }  
 
 
 
	posted on 
2011-09-21 13:59 
viste_happy 
阅读(
1495) 
评论() 
 
收藏 
举报