zoukankan      html  css  js  c++  java
  • 我的一个异步操作UML

    Reference:
    Mcad学习笔记之异步编程(AsyncCallback委托,IAsyncResult接口,BeginInvoke方法,EndInvoke方法的使用小总结)

    http://www.cnblogs.com/aierong/archive/2005/05/25/162308.html

     Mcad学习笔记之委托再理解(delegate的构造器,BeginInvoke,EndInvoke,Invoke4个方法的探讨)

    http://www.cnblogs.com/aierong/archive/2005/05/25/162181.html

    简直是经典的文章。一下子解决了我对IAsyncResult的理解。

    实际上ASP.NET的异步模式可以有4种,灵活运用IAsyncResult提供的函数能够实现。我将使用AsyncEventHandler这个代理完成我所有的异步操作。

     
    缓冲层部分核心代码:
    public delegate void AsyncEventHandler();//声明一个异步代理
    public class ConnectionPipe
    {
    private event AsyncEventHandler asy;//实例化代理
    private ContentFetcher fetcher;//获取数据类
    public IAsyncResult BeginCalling(AsyncCallback cb, string param)
    {
    if (cb == null)
    throw new Exception("Missing Para!");
    if (param == null || param == "")
    throw new Exception("Missing Para!");
    fetcher 
    = new ContentFetcher(PathMappingController.Instance.getPath(param));
    asy 
    = new AsyncEventHandler(fetcher.Fetching);
    return asy.BeginInvoke(new AsyncCallback(cb), this);
    }


    public void EndCalling(IAsyncResult ar)
    {
    if (asy == null)
    throw new Exception("Run BeginCalling First!");
    asy.EndInvoke(ar);
    }


    public string Content
    {
    get return fetcher.Content; }
    }

    }
    页面代码:
    public partial class SubPages_03projects_Lenovo : System.Web.UI.Page
    {
    protected string pageContent;
    protected void Page_Load(object sender, EventArgs e)
    {
    AddOnPreRenderCompleteAsync(
    new BeginEventHandler(BeginAsyncOperation),
    new EndEventHandler(EndAsyncOperation)
    );
    }

    IAsyncResult BeginAsyncOperation(
    object sender, EventArgs e,
    AsyncCallback cb, 
    object state)
    {
    ConnectionPipe pipe 
    = new ConnectionPipe();
    return pipe.BeginCalling(cb, "testme");
    }

    void EndAsyncOperation(IAsyncResult ar)
    {
    (ar.AsyncState 
    as ConnectionPipe).EndCalling(ar);
    pageContent 
    = (ar.AsyncState as ConnectionPipe).Content;
    }

    }
    思想:
    。页面启动,异步调用ConnectionPipe获取数据。
    。当数据获取完毕,自动调用结束。
    。使用了代理里面提供的异步操作:BeginInvoke,EndInvoke,这样我不要自己实现接口IAsyncResult.
    。当代理的方法操作完成,代理会自动调用AsyncCallback。这个省了我很多麻烦。
    asp.net异步学习完毕。
  • 相关阅读:
    Algebra, Topology, Differential Calculus, and Optimization Theory For Computer Science and Machine Learning 第8章 读书笔记(待更新)
    Webpack 2 视频教程 007
    Hie with the Pie
    P4550 收集邮票
    Bloodsucker
    P2627 [USACO11OPEN]Mowing the Lawn G
    SP1026 FAVDICE
    HDU4405 Aeroplane chess
    Card Collector
    LOOPS(概率dp)
  • 原文地址:https://www.cnblogs.com/zc22/p/554038.html
Copyright © 2011-2022 走看看