Asp.net 2.0 page performance considerations-asynchronous page Processing Model

Source: Internet
Author: User
Tags net thread
In general, we usually declare each processing process step by step on a page based on the page event processing model. This is synchronous. We can easily define the order of page processing. Although synchronous processing is very convenient, it exposes serious problems when processing time-consuming and high-request. In a highly concurrent website production server. net Processing is based on the so-called threadpool, and the processing threads in threadpool are limited. If all the threads in the threadpool have been used up and there are continuous and large-scale requests subsequently, the performance of the server will be seriously reduced, and even worse, the server will crash. Therefore, we are most concerned about how to better use the. NET threadpool. To improve the performance of. net.

Speaking of this, we have to talk about it. net asynchronous processing (asynchronous), the model is generally beginxxx, endxxx, beginxxx returns an iasycresult object, which contains information about the current asynchronous operation, and the endxxx user accepts the return value and outputs parameters .. Net allocates an idle thread from threadpool to beginxxx and returns it to threadpool immediately. When asynchronous processing ends,. Net allocates a idle thread from threadpool to process the endxxx method. In this way, there may be enough threads to handle other things. Here we only talk about Asp.net 2.0's asynchronous processing model. We all know that. it is troublesome to set up asynchronous page processing well during the process of X. Maybe Microsoft has long recognized this point, so the new page processing model introduced in version 2.0 is, A convenient asynchronous point is added between the prerender event and the prerendercomplete event. At this asynchronous point, the page needs to wait until all asynchronous processing is completed, therefore, you can achieve any effect you need before page rendering, greatly simplifying the method of creating an asynchronous page.

First, you must add async = "true" to the @ page declaration of the created page. This is required. The setting tells Asp.net to use ihttpasynchandler to process the current page. What you need to do next is generally to use Asynchronous processing in page_load event processing. There are two ways:

The first method is to use the page. addonprerendercompleteasync method to process the begin and end method sets that require asynchronous processing:

Addonprerendercompleteasync (New begineventhandler (mybeginmethod), new endeventhandler (myendmethod ));
After adding the preceding method, the page executes the usual lifecycle events until the prerender event of the page is triggered. Then Asp.net calls the begin process previously registered in addonprerendercompleteasync.Program. Usually some asynchronous Web Services, Io and SQL processing are processed in the begin processing program, which can greatly relieve the pressure on the. NET thread pool. For example:
<% @ Page async = "true" Language = "C #" %>
<Asp: Content ID = "content" contentplaceholderid = "Main" runat = "server">
<Asp: Label id = "output" runat = "server"> </ASP: Label>
</ASP: content>

Public partial class asyncpage: system. Web. UI. Page
{
Private webrequest m_request;

Void page_load (Object sender, eventargs E)
{
Addonprerendercompleteasync (
New begineventhandler (beginasyncoperation ),
New endeventhandler (endasyncoperation)
);
}

Iasyncresult beginasyncoperation (Object sender, eventargs E, asynccallback CB, object state)
{
M_request = webrequest. Create ("http://www.dofor.cn ");
Return m_request.begingetresponse (CB, State );
}

Void endasyncoperation (iasyncresult AR)
{
String text;
Using (webresponse response = m_request.endgetresponse (AR ))
{
Using (streamreader reader = new streamreader (response. getresponsestream ()))
{
TEXT = reader. readtoend ();
}
}

RegEx = new RegEx ("href \ s * = \ s * \" ([^ \ "] *) \" ", regexoptions. ignorecase );
Matchcollection matches = RegEx. Matches (text );

Stringbuilder builder = new stringbuilder (1024 );
Foreach (match in matches)
{
Builder. append (match. Groups [1]);
Builder. append ("<br/> ");
}

Output. Text = builder. tostring ();
}
}

The second is to register an asynchronous task (register asynchronous task). registerasynctask has more flexibility and advantages than addonprerendercompleteasync. It allows you to declare a time-out parameter, or declare asynctimeout = "5" in seconds in @ page, however, it should be noted that it is declared that not every asynchronous processing process times out, but the processing time of the entire page times out. Similarly,. NET Framework 2.0 introduces a new methodasync for the registration task. methodasync is designed to facilitate processing of multiple asynchronous processes. For example:
<% @ Page async = "true" Language = "C #" %>
<Asp: Content ID = "content" contentplaceholderid = "Main" runat = "server">
<Asp: Label id = "output" runat = "server"> </ASP: Label>
</ASP: content>

Public partial class asyncpagetask: system. Web. UI. Page
{
Private webrequest m_request;

Protected void page_load (Object sender, eventargs E)
{
Pageasynctask task = new pageasynctask (
New begineventhandler (beginasyncoperation ),
New endeventhandler (endasyncoperation ),
New endeventhandler (timeoutasyncoperation ),
Null
);

Registerasynctask (task );
}

iasyncresult beginasyncoperation (Object sender, eventargs E, asynccallback CB, object state)
{< br> m_request = webrequest. create ("http://www.dofor.cn");
return m_request.begingetresponse (CB, State);
}

Void endasyncoperation (iasyncresult AR)
{
String text;
Using (webresponse response = m_request.endgetresponse (AR ))
{
Using (streamreader reader = new streamreader (response. getresponsestream ()))
{
TEXT = reader. readtoend ();
}
}

RegEx = new RegEx ("href \ s * = \ s * \" ([^ \ "] *) \" ", regexoptions. ignorecase );
Matchcollection matches = RegEx. Matches (text );

Stringbuilder builder = new stringbuilder (1024 );
Foreach (match in matches)
{
Builder. append (match. Groups [1]);
Builder. append ("<br/> ");
}

Output. Text = builder. tostring ();
}

Void timeoutasyncoperation (iasyncresult AR)
{
Output. Text = "the current data is unavailable ";
}
}

In short, the rational use of Asp.net 2.0 asynchronous processing can greatly improve the performance of your high-throughput and high-concurrency website pages.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.