1. 程式人生 > >C# 基礎-異步CallBack

C# 基礎-異步CallBack

AD IT ++ man bsp lba ret C# clas

技術分享圖片
 1 delegate int runDelegate(string name);  
 2      static runDelegate run= new runDelegate(Calc);  
 3      static int count = 0;  
 4      private static int Calc(string name)  
 5      {  
 6          for (int i = 0; i < 3; i++)  
 7          System.Threading.Thread.Sleep(1000);  
 8          Console.WriteLine(System.Threading.Thread.CurrentThread.ManagedThreadId);  
9 Console.WriteLine("IsThreadPoolThread:" + 10 System.Threading.Thread.CurrentThread.IsThreadPoolThread); 11 Console.WriteLine("IsBackground:" + 12 System.Threading.Thread.CurrentThread.IsBackground); 13 return 1; 14 }
View Code

技術分享圖片
1 private
static void FinishCallBack(IAsyncResult ar) 2 { 3 Console.WriteLine("Result:" + run.EndInvoke(ar)); 4 Console.WriteLine("AsyncState:"+ ar.AsyncState); 5 if (count < 10) 6 run.BeginInvoke("zeng", FinishCallBack, "test"); 7 count++; 8 }
View Code

技術分享圖片
1 static
void Main(string[] args) 2 { 3 run.BeginInvoke("zeng", FinishCallBack, "test"); 4 Console.ReadLine(); 5 }
View Code

C# 基礎-異步CallBack