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

简介: EAP是针对Windows窗体开发提供的方便使用的异步模式,可以在IDE中可视化的设计和使用             // The System.Net.WebClient class supports the Event-based Asynchronous Pattern          ...

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

相关实践学习
通过轻量消息队列(原MNS)主题HTTP订阅+ARMS实现自定义数据多渠道告警
本场景将自定义告警信息同时分发至多个通知渠道的需求,例如短信、电子邮件及钉钉群组等。通过采用轻量消息队列(原 MNS)的主题模型的HTTP订阅方式,并结合应用实时监控服务提供的自定义集成能力,使得您能够以简便的配置方式实现上述多渠道同步通知的功能。
相关文章
|
移动开发 监控 Android开发
Android & iOS 使用 ARMS 用户体验监控(RUM)的最佳实践
本文主要介绍了 ARMS 用户体验监控的基本功能特性,并介绍了在几种常见场景下的最佳实践。
1480 120
|
运维 监控 数据可视化
ARMS的微服务监控
【8月更文挑战第23天】
389 6
|
监控 Java 索引
APM Server监控
APM Server监控
|
12月前
|
监控 Kubernetes Java
使用 New Relic APM 和 Kubernetes Metrics 监控 EKS 上的 Java 微服务
在阿里云AKS上运行Java微服务常遇性能瓶颈与OOMKilled等问题。本文教你通过New Relic实现集群与JVM双层监控,集成Helm部署、JVM代理注入、GC调优及告警仪表盘,打通从节点资源到应用内存的全链路观测,提升排障效率,保障服务稳定。
752 115
|
Kubernetes 监控 安全
Kustomize 生产实战 - 注入监控 APM Agent
Kustomize 生产实战 - 注入监控 APM Agent
|
监控 前端开发 JavaScript
ARMS的Web应用监控
【8月更文挑战第23天】
371 8
|
监控 JavaScript 前端开发
ARMS的移动应用监控
【8月更文挑战第23天】
541 6
|
监控 开发工具 Android开发
ARMS 用户体验监控正式发布原生鸿蒙应用 SDK
阿里云 ARMS 用户体验监控(RUM)推出了针对原生鸿蒙应用的 SDK。SDK 使用 ArkTS 语言开发,支持页面采集、资源加载采集、异常采集及自定义采集等功能,能够全面监控鸿蒙应用的表现。集成简单,只需几步即可将 SDK 接入项目中,为鸿蒙应用的开发者提供了强有力的支持。
1003 132
|
消息中间件 监控 Java
消息队列和应用工具产品体系-ARMS 监控种类简介(2)
消息队列和应用工具产品体系-ARMS 监控种类简介(2)
消息队列和应用工具产品体系-ARMS 监控种类简介(2)