国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

線程之使用線程回調方法

2023-05-11 12:02:28
字體:
來源:轉載
供稿:網友

此示例演示使用線程回調方法

using System;
using System.Threading;
using System.Runtime.Remoting.Messaging;

namespace 回調

 //委托聲明(函數簽名)
 delegate string MyMethodDelegate();

 class MyClass
 {
  //調用的方法
  public static string MyMethod()
  {   
   //Console.WriteLine(System.Threading.Thread.CurrentThread.IsBackground);
   for(int i = 0;i < 3; i++)  //延長時間(模擬實際任務)
   {
    Thread.Sleep(1000);
   }
   return "Hello Word";
  }
  
  //聲明委托,調用MyMethod
  private static MyMethodDelegate d = new MyMethodDelegate(MyClass.MyMethod);
  
  //聲明委托,調用AsyncCallbackMethod
  private static System.AsyncCallback a = new System.AsyncCallback(MyClass.AsyncCallbackMethod);  
  
  [STAThread]
  static void Main(string[] args)
  {
   d.BeginInvoke(a,null); 
   Console.ReadLine();   //這句不能去掉,否則主線程執行完成后,子線會會強迫調用Abort()方法銷毀掉,也就執行不到回調方法了
  }  
  
  public static void AsyncCallbackMethod(System.IAsyncResult myIAsyncResult)
  {
   string strEnd = d.EndInvoke(myIAsyncResult);      //委托調用的方法已經完成,輸出其值  
   Console.WriteLine(strEnd);
   Console.Read();
  }
 }
}

本示例代碼已經測試,能夠正常運行!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 青海省| 青河县| 合水县| 东阿县| 繁峙县| 清河县| 湄潭县| 安宁市| 大兴区| 新干县| 山东省| 北辰区| 阜城县| 武强县| 勐海县| 额尔古纳市| 南宁市| 南陵县| 满洲里市| 高碑店市| 佛山市| 东光县| 双峰县| 余姚市| 龙口市| 崇仁县| 旬阳县| 三江| 安陆市| 沅陵县| 阳朔县| 海安县| 镇康县| 璧山县| 衡阳县| 赤城县| 东明县| 红安县| 合阳县| 和静县| 黔西|