近期项目中使用了不少异步操作,关于“异步”做个总结。总结的内容大部分都来自于MSDN,还有一些自己的心得。

关于“异步”的使用可分为:使用层面和类库设计层面,细分如下:

一、使用异步方式调用同步方法(使用层面)。

二、使用 IAsyncResult 调用异步方法(使用层面)。

三、基于 IAsyncResult 的异步设计模式(设计层面)。

四、基于事件的异步模式(设计层面)。

关于上述异步编程的几个方面,下面分别做以详述。

一、使用异步方式调用同步方法(使用层面)

.NET Framework 允许您异步调用任何方法。为此,应定义与您要调用的方法具有相同签名的委托;公共语言运行库会自动使用适当的签名为该委托定义 BeginInvoke 和 EndInvoke 方法。BeginInvoke 方法可启动异步调用,EndInvoke 方法检索异步调用的结果。调用 BeginInvoke 后可随时调用 EndInvoke 方法;如果异步调用尚未完成,EndInvoke 将一直阻止调用线程,直到异步调用完成后才允许调用线程执行。

该调用方式是使用委托进行异步调用,其实质是:调用“BeginInvoke”方法,公共语言运行库 (CLR) 将对请求进行排队并立即返回到调用方。将对来自线程池的线程调用该目标方法。提交请求的原始线程自由地继续与目标方法并行执行,该目标方法是在线程池线程运行的。

在异步调用启动后,在等待异步调用结果的时候,可以进行以下四种方法。

·                     进行某些操作,然后调用 EndInvoke 一直阻止到调用完成。

  1. using System;
  2. using System.Threading;
  3. namespace Examples.AdvancedProgramming.AsynchronousOperations
  4. {
  5. public class AsyncMain
  6. {
  7. public static void Main()
  8. {
  9. // The asynchronous method puts the thread id here.
  10. int threadId;
  11. // Create an instance of the test class.
  12. AsyncDemo ad = new AsyncDemo();
  13. // Create the delegate.
  14. AsyncMethodCaller caller = new AsyncMethodCaller(ad.TestMethod);
  15. // Initiate the asychronous call.
  16. IAsyncResult result = caller.BeginInvoke(3000,
  17. out threadId, null, null);
  18. Thread.Sleep(0);
  19. Console.WriteLine("Main thread {0} does some work.",
  20. Thread.CurrentThread.ManagedThreadId);
  21. // 调用此方法将会一直等待异步操作完成,并阻塞当前线程。
  22. string returnValue = caller.EndInvoke(out threadId, result);
  23. Console.WriteLine("The call executed on thread {0}, with return value \"{1}\".",
  24. threadId, returnValue);
  25. }
  26. }
  27. }

·                     进行某些操作,然后使用
IAsyncResult..::.AsyncWaitHandle 属性获取 WaitHandle,使用它的 WaitOne
方法(该方法可以设置一个超时时间)一直阻止执行直到发出 WaitHandle 信号,然后调用 EndInvoke。

  1. using System;
  2. using System.Threading;
  3. namespace Examples.AdvancedProgramming.AsynchronousOperations
  4. {
  5. public class AsyncMain
  6. {
  7. static void Main()
  8. {
  9. // The asynchronous method puts the thread id here.
  10. int threadId;
  11. // Create an instance of the test class.
  12. AsyncDemo ad = new AsyncDemo();
  13. // Create the delegate.
  14. AsyncMethodCaller caller = new AsyncMethodCaller(ad.TestMethod);
  15. // Initiate the asychronous call.
  16. IAsyncResult result = caller.BeginInvoke(3000,
  17. out threadId, null, null);
  18. Thread.Sleep(0);
  19. Console.WriteLine("Main thread {0} does some work.",
  20. Thread.CurrentThread.ManagedThreadId);
  21. // 调用此方法将会一直等待异步操作完成,并阻塞当前线程。
  22. result.AsyncWaitHandle.WaitOne();
  23. // Perform additional processing here.
  24. // Call EndInvoke to retrieve the results.
  25. string returnValue = caller.EndInvoke(out threadId, result);
  26. Console.WriteLine("The call executed on thread {0}, with return value \"{1}\".",
  27. threadId, returnValue);
  28. }
  29. }
  30. }

·                     进行某些操作,然后轮询由 BeginInvoke 返回的 IAsyncResult,确定异步调用何时完成,然后调用 EndInvoke。

  1. using System;
  2. using System.Threading;
  3. namespace Examples.AdvancedProgramming.AsynchronousOperations
  4. {
  5. public class AsyncMain
  6. {
  7. static void Main()
  8. {
  9. // The asynchronous method puts the thread id here.
  10. int threadId;
  11. // Create an instance of the test class.
  12. AsyncDemo ad = new AsyncDemo();
  13. // Create the delegate.
  14. AsyncMethodCaller caller = new AsyncMethodCaller(ad.TestMethod);
  15. // Initiate the asychronous call.
  16. IAsyncResult result = caller.BeginInvoke(3000,
  17. out threadId, null, null);
  18. // 等待结果返回
  19. while (result.IsCompleted == false)
  20. {
  21. Thread.Sleep(10);
  22. }
  23. // Call EndInvoke to retrieve the results.
  24. string returnValue = caller.EndInvoke(out threadId, result);
  25. Console.WriteLine("The call executed on thread {0}, with return value \"{1}\".",
  26. threadId, returnValue);
  27. }
  28. }
  29. }

·                     将用于回调方法的委托传递给 BeginInvoke。异步调用完成后,将在 ThreadPool 线程上执行该方法。该回调方法将调用 EndInvoke。注意:该回调函数的执行是在另外一个线程上。

  1. using System;
  2. using System.Threading;
  3. namespace Examples.AdvancedProgramming.AsynchronousOperations
  4. {
  5. public class AsyncMain
  6. {
  7. // Asynchronous method puts the thread id here.
  8. private static int threadId;
  9. static void Main() {
  10. // Create an instance of the test class.
  11. AsyncDemo ad = new AsyncDemo();
  12. // Create the delegate.
  13. AsyncMethodCaller caller = new AsyncMethodCaller(ad.TestMethod);
  14. // Initiate the asychronous call.  Include an AsyncCallback
  15. // delegate representing the callback method, and the data
  16. // needed to call EndInvoke.
  17. IAsyncResult result = caller.BeginInvoke(3000,
  18. out threadId,
  19. new AsyncCallback(CallbackMethod),
  20. caller );
  21. Console.WriteLine("Press Enter to close application.");
  22. Console.ReadLine();
  23. }
  24. // Callback method must have the same signature as the
  25. // AsyncCallback delegate.
  26. static void CallbackMethod(IAsyncResult ar)
  27. {
  28. // Retrieve the delegate.
  29. AsyncMethodCaller caller = (AsyncMethodCaller) ar.AsyncState;
  30. // Call EndInvoke to retrieve the results.
  31. string returnValue = caller.EndInvoke(out threadId, ar);
  32. Console.WriteLine("The call executed on thread {0}, with return value \"{1}\".",
  33. threadId, returnValue);
  34. }
  35. }
  36. }

二、使用 IAsyncResult 调用异步方法(使用层面)

.NET Framework 的许多方面都支持异步编程功能,这些方面包括:

·                     文件 IO、流 IO、套接字 IO。

·                     网络。

·                     远程处理信道(HTTP、TCP)和代理。

·                     使用 ASP.NET 创建的 XML Web services。

·                     ASP.NET Web 窗体。

·                     使用 MessageQueue 类的消息队列。

这些已经实现了异步操作的类或组件,可以直接调用其异步方法(以Begin和End开头)。调用异步方法后等待结果可以参考“异步调用同步方法”的处理。

三、基于 IAsyncResult 的异步设计模式(设计层面)

待续...

最新文章

  1. JavaScript动画-碰撞检测
  2. Windows下,MySQL root用户忘记密码解决方案
  3. .NET 反射概述
  4. NuGet及快速安装使用
  5. HNU 12869 Sequence(循环节)
  6. SVD奇异值分解
  7. linux系统:rm-rf执行以后,怎么办?我来教你恢复文件。
  8. awk中文手册
  9. jQuery中的join方法
  10. RAID级别与规范
  11. 有向图的邻接矩阵表示法(创建,DFS,BFS)
  12. RDCの自我介绍
  13. Error:ivalue require as left operant of assignment
  14. linux下activemq安装与配置
  15. JVM性能调优监控命令jps、jinfo、jstat、jmap+jhat、jstack使用详解
  16. [HDU5969] 最大的位或
  17. list源码3(参考STL源码--侯捷):push_front、push_back、erase、pop_front、pop_back、clear、remove、unique
  18. 读jQuery源码释疑笔记3
  19. _cs, _ci, or _bin,
  20. Java编程语言下 Selenium 驱动各个浏览器代码

热门文章

  1. 如何在OS X 10.9 Mavericks下安装Command Line Tools(命令行工具)
  2. Masking operations
  3. 16_Java正则和日期对象
  4. Flask之视图(二)
  5. C投票系统
  6. Service通信的两篇博文
  7. [转] C#中out和ref之间的区别
  8. swagger 接口文档,控制器 和 object类型的参数与返回值 的 注释不显示问题
  9. Elasticsearch-PHP 索引操作
  10. 深度学习篇——Tensorflow配置(傻瓜安装模式)