asp.net 2.0 异步页

References:
 
Introduction:
在asp.net2.0提供了异步页的支持。具体参考上文。这里简单叙述一下。
1)页面接受用户请求,使用调用WebRequest获取另外一个网站的信息。
2)当获取完毕,页面再次处理获取结果,返回用户。
3)过程中的获取是异步的,服务器不始终保持和用户的联系,解放了线程池。
 
 
None.gif using  System;
None.gif
using  System.Web;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Net;
None.gif
using  System.IO;
None.gif
using  System.Text;
None.gif
using  System.Text.RegularExpressions;
None.gif
None.gif
public  partial  class  AsyncPage : System.Web.UI.Page
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
private WebRequest _request;
InBlock.gif
InBlock.gif    
void Page_Load (object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        AddOnPreRenderCompleteAsync (
InBlock.gif            
new BeginEventHandler(BeginAsyncOperation),
InBlock.gif            
new EndEventHandler (EndAsyncOperation)
InBlock.gif        );
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    IAsyncResult BeginAsyncOperation (
object sender, EventArgs e,
InBlock.gif        AsyncCallback cb, 
object state)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        _request 
= WebRequest.Create("http://msdn.microsoft.com");
InBlock.gif        
return _request.BeginGetResponse (cb, state);
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
void EndAsyncOperation (IAsyncResult ar)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
string text;
InBlock.gif        
using (WebResponse response = _request.EndGetResponse(ar))
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
using (StreamReader reader =
InBlock.gif                
new StreamReader(response.GetResponseStream()))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                text 
= reader.ReadToEnd();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        Regex regex 
= new Regex ("href\\s*=\\s*\"([^\"]*)\"",
InBlock.gif
            RegexOptions.IgnoreCase);
InBlock.gif        MatchCollection matches 
= regex.Matches(text);
InBlock.gif
InBlock.gif        StringBuilder builder 
= new StringBuilder(1024);
InBlock.gif        
foreach (Match match in matches)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            builder.Append (match.Groups[
1]);
InBlock.gif            builder.Append(
"<br/>");
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        Output.Text 
= builder.ToString (); //Output是页面一个Label
ExpandedSubBlockEnd.gif
    }

ExpandedBlockEnd.gif}
 
红色部分是重点。大概流程:
。aspx 主线程接受到了用户请求,打开异步模式处理BeginAsyncOperation。
。在方法BeginAsyncOperation里面,执行异步WebRequest.Create,并且返回一个 IAsyncResult主线程 。主线程停止,回收到线程池。
。当WebRequest处理完毕后,通过IAsyncResult通知回主线程主线程再次接管,调用EndAsyncOperation 处理剩下的部分。

转载于:https://www.cnblogs.com/zc22/archive/2006/11/08/554008.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值