EAP是针对Windows窗体开发提供的方便使用的异步模式,可以在IDE中可视化的设计和使用

            // The System.Net.WebClient class supports the Event-based Asynchronous Pattern
            WebClient wc = new WebClient();
 
            // When a string completes downloading, the WebClient object raises the 
            // DownloadStringCompleted event which will invoke our ProcessString method         
            wc.DownloadStringCompleted += (s, e) =>
            {
                System.Windows.Forms.MessageBox.Show((e.Error != null) ? e.Error.Message : e.Result);
            };
 
            // Start the asynchronous operation (this is like calling a BeginXxx method)
            wc.DownloadStringAsync(new Uri("http://Wintellect.com"));

.net有很多的类是这个模式

System.ComponentModel.Component-derived types

System.ComponentModel.BackgroundWorker

System.Media.SoundPlayer

System.Net.WebClient

System.Net.NetworkInformation.Ping

System.Windows.Forms.PictureBox (derived from Control)

System.Object-derived types

System.Net.Mail.SmtpClient

System.Deployment.Application.ApplicationDeployment

System.Deployment.Application.InPlaceHostingManager

System.Activities.WorkflowInvoker

System.ServiceModel.Activities.WorkflowControlClient

System.Net.PeerToPeer.PeerNameResolver

System.Net.PeerToPeer.Collaboration.ContactManager

System.Net.PeerToPeer.Collaboration.Peer

System.Net.PeerToPeer.Collaboration.PeerContact

System.Net.PeerToPeer.Collaboration.PeerNearMe

System.ServiceModel.Discovery.AnnouncementClient

System.ServiceModel.Discovery.DiscoveryClient

使用TaskCompletionSource将EAP转换为Task

// The System.Net.WebClient class supports the Event-based Asynchronous Pattern
            WebClient wc = new WebClient();

// Create the TaskCompletionSource and its underlying Task object
            var tcs = new TaskCompletionSource<String>();

// When a string completes downloading, the WebClient object raises the 
            // DownloadStringCompleted event which will invoke our ProcessString method
            wc.DownloadStringCompleted += (sender, ea) =>
            {
                // This code always executes on the GUI thread; set the Task’s state
                if (ea.Cancelled) tcs.SetCanceled();
                else if (ea.Error != null) tcs.SetException(ea.Error);
                else tcs.SetResult(ea.Result);
            };

// Have the Task continue with this Task that shows the result in a message box
            // NOTE: The TaskContinuationOptions.ExecuteSynchronously flag is required to have this code
            // run on the GUI thread; without the flag, the code runs on a thread pool thread 
            tcs.Task.ContinueWith(t =>
            {
                try
                {
                    System.Windows.Forms.MessageBox.Show(t.Result);
                }
                catch (AggregateException ae)
                {
                    System.Windows.Forms.MessageBox.Show(ae.GetBaseException().Message);
                }
            }, TaskContinuationOptions.ExecuteSynchronously);

// Start the asynchronous operation (this is like calling a BeginXxx method)
            wc.DownloadStringAsync(new Uri("http://Wintellect.com"));

APM之EAP APM比较、

优点

缺点

EAP

可以和Visual Stuido配合使用,提供设计时支持

EAP是通过SynchronizationContext处理线程模型的,因此GUI程序中使用方便

比APM消耗的更多地内存、更慢的速度

EAP的异常不会抛出

APM

APM更接近事物本质

EAP内部一般都是用APM事先

-

详细参考:

Clr Via C#

http://transbot.blog.163.com

http://ys-f.ys168.com/?CLR_via_CSharp_3rd_Edition_Code_by_Jeffrey_Richter.zip_55bism1e0e7bkisjthit2bso0cm5bs4bs1b5bktnql0c0bu22f05f12z

转载于:https://www.cnblogs.com/2018/archive/2011/05/11/2040334.html

APM之基于事件的异步模式(EAP)-2相关推荐

  1. F#与ASP.NET(1):基于事件的异步模式与异步Action

    提高ASP.NET应用程序伸缩性的有效手段之一便是使用异步请求.而在ASP.NET MVC 1中是不能直接支持异步Action的,因此我们需要使用一些简单的Hack方式来实现这一点.不过简单的Hack ...

  2. 基于事件的异步模式——BackgroundWorker

    转自strangeman原文 基于事件的异步模式--BackgroundWorker 实现异步处理的方法很多,经常用的有基于委托的方式,今天记录的是基于事件的异步模式.利用BackgroundWork ...

  3. C# 基于事件的异步模式

    点击蓝字 关注我们 开工大吉 EventBasedAsyncPattern 方法使用了基于事件的异步模式.这个模式定义了一个带有 "Async" 后缀的方法.示例代码再次使用了We ...

  4. 基于事件的异步模式概述

    基于事件的异步模式概述 MSDN 那些同时执行多项任务.但仍能响应用户交互的应用程序通常需要实施一种使用多线程的设计方案.System.Threading 命名空间提供了创建高性能多线程应用程序所必需 ...

  5. 【转】1.7异步编程:基于事件的异步编程模式(EAP)

    传送门:异步编程系列目录-- 上一篇,我给大家介绍了".NET1.0 IAsyncResult异步编程模型(APM)",通过Begin*** 开启操作并返回IAsyncResult ...

  6. 基于任务的异步模式(TAP)

    Task .net 4.0为我们带来了Task的异步,我们有以下三种方法创建Task. 1,Task.Factory.StartNew,比较常用. 2,Task.Run,是.net 4.5中增加的. ...

  7. 与其他.Net异步模式和类型进行互操作

    返回该系列目录<基于Task的异步模式--全面介绍> Tasks和异步编程模型APM(Tasks and the Asynchronous Programming Model) 从APM到 ...

  8. .NET三种异步模式(APM、EAP、TAP)

    APM模式: .net 1.0时期就提出的一种异步模式,并且基于IAsyncResult接口实现BeginXXX和EndXXX类似的方法. .net中有很多类实现了该模式(比如HttpWebReque ...

  9. 【转】异步编程:.NET 4.5 基于任务的异步编程模型(TAP)

    最近我为大家陆续介绍了"IAsyncResult异步编程模型 (APM)"和"基于事件的异步编程模式(EAP)"两种异步编程模型.在.NET4.0 中Micro ...

最新文章

  1. java orm 工具_GitHub - donnie4w/jdao: jdao是一个java的轻量级orm工具包
  2. tensorflow学习笔记——使用TensorFlow操作MNIST数据(1)
  3. JavaScript进阶2-学习笔记
  4. 90% 程序员都吃亏在这门技术上了,你呢!
  5. 测试总结(部分)---转载
  6. 你能卖什么,决定了你的收入落在什么档次
  7. 可涂鸦音乐光立方(DIY)
  8. CE教程:植物大战僵尸(太阳数值修改)
  9. st7789屏幕使用方法
  10. 赢富、超赢TopView SuperView TotalView 数据网站
  11. FFmpeg完美编译iOS版本
  12. [Unity][Crowd]学习人群模拟资源分享以及相关的问题
  13. UCI、KEEL下载数据集
  14. bat 2018自然语言处理校园招聘的要求
  15. 诺思格医药通过注册:年营收6亿 实控人武杰为美国籍
  16. 【iOS开发】AFNetworking上传语音文件(.mp3)到服务器
  17. 倒霉。。。倒霉。。。。。
  18. itoa和aoti函数的自我实现
  19. 盛科V680-TAP系列交换机
  20. android sdk下载地址

热门文章

  1. linux c编程项目实例,Linux c编程实例_例子
  2. nstimer循环引用_解决NSTimer循环引用导致内存泄漏的六种方法
  3. 安卓开发 底部导航图标切换时动画效果_安卓10系统终于来了,流畅度堪比苹果?...
  4. java文件名要和什么一致,Java的类名与文件名必须一致
  5. python索引序列_Pythonfor循环通过序列索引迭代过程解析
  6. oracle rodm包,由重启引起的Oracle RAC节点宕机分析及追根溯源
  7. 实现php实现价格的排序,PHP实现二维数组排序(按照数组中的某个字段)
  8. python diango 并发_利用gunicorn提高django的并发能力
  9. ege函数库_EGE图形库|EGE图形库下载v12.11 最新版 附使用教程 - 欧普软件下载
  10. mysql ib_logfile 数量_Mysql 事务日志(Ib_logfile)