我的一个异步操作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异步学习完毕。
posted @ 2006-11-08 13:06    阅读(806)  评论(0编辑  收藏  举报
IT民工