zoukankan      html  css  js  c++  java
  • APM之基于事件的异步模式(EAP)2

    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

  • 相关阅读:
    共创力董事长杨学明先生受邀参加CED智慧大会!
    杨学明老师为华宇金信(北京)软件有限公司提供为期两天的内训服务!
    《互联网敏捷测试管理实践》课程大纲 2018.12.15~16 (上海)
    共创力咨询杨学明老师为国电南瑞提供两天的内训服务!
    《软件测试管理》深圳公开课预告 2018.11.23~24 中南海滨大酒店
    软件测试管理的十大挑战
    对于开发人员修改代码引发新问题的处理措施
    热烈庆祝杨学明老师为上海某著名金融互联网公司提供两天的内训服务!
    《高效的互联网研发项目管理》课程大纲
    《软件测试管理》北京公开课预告 2018.9.28~29
  • 原文地址:https://www.cnblogs.com/2018/p/2040334.html
Copyright © 2011-2022 走看看