欢迎光临
我们一直在努力

(四)线程–使用线程回调方法-.NET教程,算法/线程

建站超值云服务器,限时71元/月

 

(一).描述
   此示例演示使用线程回调方法
(二).代码
   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();
  }
 }
}

本示例代码已经测试,能够正常运行!

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » (四)线程–使用线程回调方法-.NET教程,算法/线程
分享到: 更多 (0)

相关推荐

  • 暂无文章