A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© sunrise2 高级黑马   /  2014-8-11 09:34  /  756 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

一个简单的对C#Web异步操作的封装,继承 MyHttpAsync 抽象类,实现 MyAsyncRun 方法即可
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Web;

  4. namespace HttpAsync
  5. {
  6.     /// <summary>
  7.     /// 封装web异步处理请求操作的方法
  8.     /// </summary>
  9.     public abstract class MyHttpAsync : IHttpAsyncHandler
  10.     {

  11.         HttpContext context;
  12.         object extraData;
  13.         internal bool _isReusable = false;

  14.         /// <summary>
  15.         /// 获取导航开始时传递的可选数据
  16.         /// </summary>
  17.         public object ExtraData
  18.         {
  19.             get { return extraData; }
  20.             //set { extraData = value; }
  21.         }

  22.         /// <summary>
  23.         /// 获取当前 HTTP 请求的所有特定信息对象
  24.         /// </summary>
  25.         public HttpContext Context
  26.         {
  27.             get { return context; }
  28.             //set { context = value; }
  29.         }

  30.         /// <summary>
  31.         /// 获取当前 HTTP 请求的 System.Web.HttpRequest 对象
  32.         /// </summary>
  33.         public HttpResponse Response
  34.         {
  35.             get { return this.context.Response; }
  36.         }

  37.         /// <summary>
  38.         /// 获取当前 HTTP 请求的 System.Web.HttpRequest 对象
  39.         /// </summary>
  40.         public HttpRequest Request
  41.         {
  42.             get { return this.context.Request; }
  43.         }

  44.         /// <summary>
  45.         /// web异步请求的入口
  46.         /// </summary>
  47.         public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, object extraData)
  48.         {
  49.             this.context = context;
  50.             this.extraData = extraData;

  51.             myIAsyncResult mr = new myIAsyncResult(cb, new delRun(MyAsyncRun));
  52.             mr.myIAsyncResultRun();
  53.             return mr;

  54.         }

  55.         /// <summary>
  56.         /// 为异步进程提供的一种立即结束方法
  57.         /// </summary>
  58.         public void EndProcessRequest(IAsyncResult result)
  59.         {
  60.             MyEndProcessRequest(result);
  61.         }

  62.         /// <summary>
  63.         /// 获取一个布尔值,它指示其他请求是否可以使用 HttpRemotingHandler。
  64.         /// </summary>
  65.         public bool IsReusable
  66.         {
  67.             get { return _isReusable; }
  68.         }

  69.         /// <summary>
  70.         /// 处理 HTTP 请求。
  71.         /// </summary>
  72.         /// <param name="context"></param>
  73.         public abstract void MyProcessRequest(HttpContext context);

  74.         /// <summary>
  75.         /// 为异步进程提供的一种立即结束方法
  76.         /// </summary>
  77.         public abstract void MyEndProcessRequest(IAsyncResult result);

  78.         /// <summary>
  79.         /// 需要异步执行的操作
  80.         /// </summary>
  81.         public abstract void MyAsyncRun();

  82.         /// <summary>
  83.         /// 处理 HTTP 请求。
  84.         /// </summary>
  85.         public void ProcessRequest(HttpContext context)
  86.         {
  87.             MyProcessRequest(context);
  88.         }
  89.     }



  90.     /// <summary>
  91.     /// 异步操作类
  92.     /// </summary>
  93.     class myIAsyncResult : IAsyncResult
  94.     {
  95.         AsyncCallback cb;
  96.         delRun dr;
  97.         bool _IsCompleted = false;

  98.         public object AsyncState
  99.         {
  100.             get { return null; }
  101.         }

  102.         public System.Threading.WaitHandle AsyncWaitHandle
  103.         {
  104.             get { return null; }
  105.         }

  106.         public bool CompletedSynchronously
  107.         {
  108.             get { return false; }
  109.         }
  110.    
  111.         public bool IsCompleted
  112.         {
  113.             get { return _IsCompleted; }
  114.         }

  115.         public void myIAsyncResultRun()
  116.         {
  117.             dr();
  118.             _IsCompleted = true;
  119.             cb(this);
  120.         }

  121.         public myIAsyncResult(AsyncCallback cb, delRun dr)
  122.         {
  123.             this.cb = cb;
  124.             this.dr = dr;
  125.         }


  126.     }

  127.     /// <summary>
  128.     /// 异步执行的委托
  129.     /// </summary>
  130.     delegate void delRun();
  131. }
复制代码


0 个回复

您需要登录后才可以回帖 登录 | 加入黑马