1 #region 异步测试 2 //委托 3 public delegate void PrintDelegate(string s); 4 [WebMethod] 5 public string yibu() 6 { 7 //主线程 主方法 8 PrintDelegate printDelegate = Print; 9 printDelegate.BeginInvoke("", PrintComeplete, printDelegate); 10 return "OK"; 11 } 12 13 public static void Print(string s) 14 { 15 //"异步线程开始执行" 异步方法 16 Service svice2 = new Service(); 17 for (int i = 0; i < 999999999; i++) 18 { 19 svice2.TestTabAdd(i.ToString()); 20 } 21 Thread.Sleep(5000); 22 } 23 //回调方法要求 24 //1.返回类型为void 25 //2.只有一个参数IAsyncResult 26 public static void PrintComeplete(IAsyncResult result) 27 { 28 (result.AsyncState as PrintDelegate).EndInvoke(result); 29 //Console.WriteLine("当前线程结束." + result.AsyncState.ToString()); 30 } 31 #endregion
时间: 2024-11-06 09:48:22