C#Web异步操作封装
using System; using System.Collections.Generic; using System.Web; namespace HttpAsync { /// <summary> /// 封装web异步处理请求操作的方法 /// </summary> public abstract class MyHttpAsync : IHttpAsyncHandler { HttpContext context; object extraData; internal bool _isReusable = false; /// <summary> /// 获取导航开始时传递的可选数据 /// </summary> public object ExtraData { get { return extraData; } //set { extraData = value; } } /// <summary> /// 获取当前 HTTP 请求的所有特定信息对象 /// </summary> public HttpContext Context { get { return context; } //set { context = value; } } /// <summary> /// 获取当前 HTTP 请求的 System.Web.HttpRequest 对象 /// </summary> public HttpResponse Response { get { return this.context.Response; } } /// <summary> /// 获取当前 HTTP 请求的 System.Web.HttpRequest 对象 /// </summary> public HttpRequest Request { get { return this.context.Request; } } /// <summary> /// web异步请求的入口 /// </summary> public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, object extraData) { this.context = context; this.extraData = extraData; myIAsyncResult mr = new myIAsyncResult(cb, new delRun(MyAsyncRun)); mr.myIAsyncResultRun(); return mr; } /// <summary> /// 为异步进程提供的一种立即结束方法 /// </summary> public void EndProcessRequest(IAsyncResult result) { MyEndProcessRequest(result); } /// <summary> /// 获取一个布尔值,它指示其他请求是否可以使用 HttpRemotingHandler。 /// </summary> public bool IsReusable { get { return _isReusable; } } /// <summary> /// 处理 HTTP 请求。 /// </summary> /// <param name="context"></param> public abstract void MyProcessRequest(HttpContext context); /// <summary> /// 为异步进程提供的一种立即结束方法 /// </summary> public abstract void MyEndProcessRequest(IAsyncResult result); /// <summary> /// 需要异步执行的操作 /// </summary> public abstract void MyAsyncRun(); /// <summary> /// 处理 HTTP 请求。 /// </summary> public void ProcessRequest(HttpContext context) { MyProcessRequest(context); } } /// <summary> /// 异步操作类 /// </summary> class myIAsyncResult : IAsyncResult { AsyncCallback cb; delRun dr; bool _IsCompleted = false; public object AsyncState { get { return null; } } public System.Threading.WaitHandle AsyncWaitHandle { get { return null; } } public bool CompletedSynchronously { get { return false; } } public bool IsCompleted { get { return _IsCompleted; } } public void myIAsyncResultRun() { dr(); _IsCompleted = true; cb(this); } public myIAsyncResult(AsyncCallback cb, delRun dr) { this.cb = cb; this.dr = dr; } } /// <summary> /// 异步执行的委托 /// </summary> delegate void delRun(); }