惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

人人都是产品经理
人人都是产品经理
博客园_首页
IT之家
IT之家
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Vercel News
Vercel News
美团技术团队
D
Docker
WordPress大学
WordPress大学
T
Tailwind CSS Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
The Cloudflare Blog
Y
Y Combinator Blog
F
Fortinet All Blogs
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
G
Google Developers Blog
爱范儿
爱范儿
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
月光博客
月光博客
MongoDB | Blog
MongoDB | Blog
S
SegmentFault 最新的问题
GbyAI
GbyAI
Hugging Face - Blog
Hugging Face - Blog
Microsoft Azure Blog
Microsoft Azure Blog
A
About on SuperTechFans

博客园 - tony.zjb

中国市场 IIC GPN14 49美元Android PC驾到!威盛APC初探 C# 16进制与字符串、字节数组之间的转换 什么是RAW数据? s3c6410 SD卡启动的Secure mode Linux常用的 wince进入回收站== 转载:在WinCE中实现Screen Rotation bsp是什么? vc技巧 _stdcall(WINAPI) 与 _cdecl的区别 用C函数来转换Unicode和ANSI文字 - tony.zjb - 博客园 Win32 字符编码 明辨接口实现和虚函数重载的区别 阅读代码 remoting 全新的2008年。。。。。。 vfpConn
异步
tony.zjb · 2008-05-28 · via 博客园 - tony.zjb

一).描述
  先运行个简单的线程示例,认识一下线程
  通过委托调用方法,以及使用AsyncResult判断线程的状态

(二).代码
using System;
using System.Threading;
using System.Runtime.Remoting.Messaging;

namespace 通过委托异步调用方法

 //委托声明(函数签名)
 delegate string MyMethodDelegate();

 class MyClass
 {
  //要调用的动态方法
  public string MyMethod1()
  {
   return "Hello Word1";
  }

  //要调用的静态方法
  public static string MyMethod2()
  {
   return "Hello Word2";
  }
 }
 class Class1
 {
  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [STAThread]
  static void Main(string[] args)
  {
            MyClass myClass = new MyClass();
   
   //方式1:  声明委托,调用MyMethod1
   MyMethodDelegate d = new MyMethodDelegate(myClass.MyMethod1);
   string strEnd = d();   
   Console.WriteLine(strEnd);

   //方式2:  声明委托,调用MyMethod2 (使用AsyncResult对象调用)
   d = new MyMethodDelegate(MyClass.MyMethod2); //定义一个委托可以供多个方法使用      
   AsyncResult myResult;   //此类封闭异步委托异步调用的结果,通过AsyncResult得到结果.
   myResult = (AsyncResult)d.BeginInvoke(null,null);        //开始调用
   while(!myResult.IsCompleted)  //判断线程是否执行完成
   {
    Console.WriteLine("正在异步执行MyMethod2 .....");
   }
   Console.WriteLine("方法MyMethod2执行完成!");
   strEnd = d.EndInvoke(myResult);      //等待委托调用的方法完成,并返回结果  
   Console.WriteLine(strEnd);
   Console.Read();
  }
 }
}