国产探花免费观看_亚洲丰满少妇自慰呻吟_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();
  }
 }
}

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 大埔县| 泗阳县| 孟州市| 迁安市| 盱眙县| 花莲市| 天全县| 郯城县| 澜沧| 微博| 融水| 个旧市| 达孜县| 石泉县| 丰都县| 建瓯市| 梁河县| 陆良县| 当阳市| 沂源县| 普兰县| 永兴县| 宁陕县| 融水| 页游| 镇宁| 东光县| 沁源县| 朝阳市| 靖宇县| 普定县| 游戏| 哈巴河县| 璧山县| 五指山市| 儋州市| 巩留县| 衢州市| 宜川县| 荔浦县| 庄浪县|