抽象出一个炒菜的类。

   1:      //炒菜
   2:      public class Cooking
   3:      {
   4:          public string Cook(string food1, string food2)
   5:          {
   6:              Thread.CurrentThread.Name = "王大厨";
   7:              Console.WriteLine("厨师开始炒菜");
   8:   
   9:              for (int i = 1; i <= 2; i++)
  10:              {
  11:                  Thread.Sleep(TimeSpan.FromSeconds(i));
  12:                  Console.WriteLine("{0}:在炒菜(时间:{1}秒)",Thread.CurrentThread.Name,i);
  13:              }
  14:              Console.WriteLine("好了,菜做完了");
  15:              return food1 + food2;
  16:          }
  17:      }

客户端调用该类和方法,只有等炒菜线程执行完才能执行后面。

   1:      class Program
   2:      {
   3:          static void Main(string[] args)
   4:          {
   5:              Console.WriteLine("客人点菜了!");
   6:   
   7:              Cooking cooking = new Cooking();
   8:              string result = cooking.Cook("鱼香", "肉丝");
   9:              Console.WriteLine("{0}好了,请慢用。", result);
  10:   
  11:              Console.WriteLine("客人吃饭");
  12:              Console.ReadKey();
  13:          }
  14:      }

我们想让客人在炒菜上来之前喝点水,嗑点瓜子......
那就把炒菜的事交给委托,客人就可以放心地做其它事了。

使用委托异步调用注册方法

   1:  using System.Threading;
   2:  using System.Threading.Tasks;
   3:   
   4:  namespace ConsoleApplication17
   5:  {
   6:      class Program
   7:      {
   8:          static void Main(string[] args)
   9:          {
  10:              Console.WriteLine("客人点菜了!");
  11:   
  12:              Cooking cooking = new Cooking();
  13:              CookDelegate del = new CookDelegate(cooking.Cook); //给委托注册方法
  14:              IAsyncResult asyncResult = del.BeginInvoke("西红柿", "鸡蛋", null, null);
  15:              Console.WriteLine("客人做点其它事......");
  16:              string result = del.EndInvoke(asyncResult);
  17:              Console.WriteLine("{0}好了,请慢用。", result);
  18:              Console.ReadKey();
  19:          }
  20:      }
  21:   
  22:      public delegate string CookDelegate(string food1, string food2);
  23:   
  24:      //炒菜
  25:      public class Cooking
  26:      {
  27:          public string Cook(string food1, string food2)
  28:          {
  29:              Thread.CurrentThread.Name = "王大厨";
  30:              Console.WriteLine("厨师开始炒菜");
  31:   
  32:              for (int i = 1; i <= 2; i++)
  33:              {
  34:                  Thread.Sleep(TimeSpan.FromSeconds(i));
  35:                  Console.WriteLine("{0}:在炒菜(时间:{1}秒)",Thread.CurrentThread.Name,i);
  36:              }
  37:              Console.WriteLine("好了,菜做完了");
  38:              return food1 + food2;
  39:          }
  40:      }
  41:  }
  42:   

委托异步调用注册方法结束后调用回调方法

   1:  using System.Runtime.Remoting.Messaging;
   2:  using System.Text;
   3:  using System.Threading;
   4:  using System.Threading.Tasks;
   5:   
   6:  namespace ConsoleApplication17
   7:  {
   8:      class Program
   9:      {
  10:          static void Main(string[] args)
  11:          {
  12:              Console.WriteLine("客人点菜了!");
  13:   
  14:              Cooking cooking = new Cooking();
  15:              CookDelegate del = new CookDelegate(cooking.Cook); //给委托注册方法
  16:   
  17:              string data = "希望您能喜欢";
  18:              AsyncCallback callBack = new AsyncCallback(OnCookComplete);
  19:              IAsyncResult asyncResult = del.BeginInvoke("西红柿", "鸡蛋", callBack, data);
  20:              Console.WriteLine("客人做点其它事......");
  21:   
  22:              Console.ReadKey();
  23:          }
  24:   
  25:          //回调需要执行的方法
  26:          static void OnCookComplete(IAsyncResult asyncResult)
  27:          {
  28:              AsyncResult result = (AsyncResult) asyncResult;
  29:   
  30:              //从异步结果中可以拿到委托
  31:              CookDelegate del = (CookDelegate)result.AsyncDelegate;
  32:   
  33:              //从异步结果中可以拿到BeginInvoke方法中的object参数
  34:              string data = (string) asyncResult.AsyncState;
  35:   
  36:              string r = del.EndInvoke(asyncResult);
  37:              Console.WriteLine("{0}好了,请慢用,{1}", r,data);
  38:          }
  39:      }
  40:   
  41:      public delegate string CookDelegate(string food1, string food2);
  42:   
  43:      //炒菜
  44:      public class Cooking
  45:      {
  46:          public string Cook(string food1, string food2)
  47:          {
  48:              Thread.CurrentThread.Name = "王大厨";
  49:              Console.WriteLine("厨师开始炒菜");
  50:   
  51:              for (int i = 1; i <= 2; i++)
  52:              {
  53:                  Thread.Sleep(TimeSpan.FromSeconds(i));
  54:                  Console.WriteLine("{0}:在炒菜(时间:{1}秒)",Thread.CurrentThread.Name,i);
  55:              }
  56:              Console.WriteLine("好了,菜做完了");
  57:              return food1 + food2;
  58:          }
  59:      }
  60:  }
  61:   

可见:
● 当使用委托异步调用注册的方法时,不会影响到客户端,客户端程序不会等
● 当异步调用委托注册的方法时,参数不仅有方法参数,还有AsyncCallback回调参数,和object对象
● 回调方法中,可以从IAsyncResult获取方法返回值,object对象

17委托异步调用方法相关推荐

  1. (二)线程--通过委托异步调用方法

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

  2. java 异步调用方法_java异步调用方法有哪些?如何实现异步调用?

    你知道java异步调用方法都有哪些吗?下面的文章内容,就对这方面的问题做了一下整理,一起来看看java异步调用的方法吧! 1.利用Spring的异步方法去执行 注:没有返回值 在启动类又或者是配置类加 ...

  3. SpringBoot异步调用方法

    SpringBoot异步调用方法 一.spring boot--使用异步请求,提高系统的吞吐量 https://blog.csdn.net/liuchuanhong1/article/details/ ...

  4. WinForm 异步调用方法

    假如要在一个线程中异步执行一个方法,则先创建一个该方法的委托类型,然后CLR会自动为该委托类型定义一个BeginInvoke方法和EndInvoke方法,我们就靠这两个方法异步调用委托类型指向的方法( ...

  5. 那些年我们一起追逐的多线程(Thread、ThreadPool、委托异步调用、Task/TaskFactory、Parallerl、async和await)

    一. 背景 在刚接触开发的头几年里,说实话,根本不考虑多线程的这个问题,貌似那时候脑子里也有没有多线程的这个概念,所有的业务都是一个线程来处理,不考虑性能问题,当然也没有考虑多线程操作一条记录存在的并 ...

  6. 异步调用方法时异常的捕获

    在异步调用一个方法时,由于不在一个线程内,异常的处理不同于普通同步调用,但可以在EndXXX时,使用try{}catch{}来捕获. public static void main(string[]  ...

  7. jquery 异步调用方法中不能给全局变量赋值的原因及解决办法

    在调用一个jquery的ajax方法时我们有时会需要该方法返回一个值或者给某个全局变量赋值,可是我们发现程序执行完后并没有获取到我们想要的值,这时很有可能是因为你用的是ajax的异步调用async:t ...

  8. C#的同步和异步调用方法

    同步和异步大家都明白什么意思,在这里不多介绍了. namespace ConsoleTest {class Program{static void Main(string[] args){Consol ...

  9. Spring注解 @Async 实现异步调用方法

    异步方法调用使用场景:处理日志.发送邮件.发送短信... 关于@Async: (1)Spring 3.0 以及以后版本中支持的@Async (2)@Async修饰类,则该类所有方法都是异步的,@Asy ...

最新文章

  1. C#选择目录对话框FolderBrowserDialog
  2. C++中构造函数和析构函数
  3. .net mvc web api 返回 json 内容时过滤值为null的属性
  4. Thread pool引起的程序连接数据库响应慢
  5. Java多线程-生产者与消费者
  6. LayoutInflater
  7. 转载:Rootkit总结
  8. awg线径与电流_AWG线径对照表
  9. 单片机蜂鸣器源代码+仿真
  10. 计算机打印机无法扫描,打印机无法扫描 打印机无法扫描怎么办 4种原因及解决方法...
  11. 小程序增加 文章 / 新闻 / 资讯 / 动态 功能,支持用户投稿
  12. Redis笔记--狂神
  13. 【python】numpy.percentile()函数
  14. bwiki样式的活动倒计时html代码(带渐变色进度条)
  15. 浪潮2020年Q1闪存存储领涨,出货量增速跃居中国第一
  16. 什么是知识图谱?通俗易懂
  17. 我对软件应聘学生的建议
  18. 拼多多店铺怎么上榜单?
  19. gradle全集 下载 蓝凑云(非百度网盘)
  20. [线性dp]leetcode2327:知道秘密的人数(medium)

热门文章

  1. 扩散模型就是自动编码器!DeepMind研究学者提出新观点并论证
  2. 量化集体行为特刊:信息论为量化集体性提供形式化框架
  3. HarmonyOS 2面世!是没有退路还是时机成熟?中国操作系统崛起元年或已到来
  4. 欧拉公式——真正的宇宙第一公式
  5. 揭秘5G+AI时代的机器人世界!七大核心技术改变人类生活
  6. SCI至上只是结果,而不是原因
  7. 刚刚,生物学横扫诺贝尔3大奖,两名女性获奖!化学奖授予试管中的“进化论”...
  8. AI企业下一个使命:让生物特征数据使用走向阳光透明
  9. 毕马威:2018全球科技创新报告(附PDF下载)
  10. 华为云BU总裁:如何把AI从噱头变为生产力?