C# 基础-异步CallBack

 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

猜你喜欢

转载自www.cnblogs.com/Lite/p/9076473.html