ASP.net 2.0 Asynchronous page refresh really hard.

Source: Internet
Author: User
Tags date execution int size socket thread
Asp.net| Refresh | page | asynchronous

The asynchronous page refresh feature of ASP.net 2.0 is really cool, and it's a big help to try to solve one of my application problems today.

My page will submit a query to the socket service in the background, first of all, it is an operation that may not return for a long time; secondly, in order to get the length of the socket service returned indefinite, potentially large response data, I have to use the dotnet IAsyncResult BeginReceive (byte[] buffer, int offset, int size, SocketFlags SocketFlags, AsyncCallback callback, Object state); method to asynchronously submit s Ocket the command, and then iterates through the call int endreceive (IAsyncResult asyncresult) in the callback function, to get the response data until the data has been fetched, and this series of data fragments is slow to exist in a memoryblock list.

In this case, the ASP. NET page to display response data in a table, it must be able to be refreshed asynchronously.

ASP.net 1.x does not support asynchronous pages in nature, but it can generate asynchronous pages through tough efforts and relentless innovation. For more overview information, see the articles in the Msdn®magazine June 2003 issue "Use Threads and build asynchronous handlers in Your Web Code", the author of the article It's Fritz Onion.

But ASP.net 2.0 greatly simplifies the way in which asynchronous pages are generated. The async= "true" attribute is introduced first using the @ Page directive of the page.

Secondly, when Page_Load, the registration event Page_PreRender:

private void Page_Load (object sender, System.EventArgs e)

{

if (! IsPostBack)

{

Registering event Page_PreRender execution method when execution completes

Hook Prerendercomplete event for data binding

This. Prerendercomplete + = new EventHandler (page_prerendercomplete);

Next, I start my asynchronous Operation tour in the event function of the click button:

The pageasynctask approach is to enhance the version, in addition to the asynchronous page processing start and end methods themselves,

You can also provide a method of processing in times of time-out, and a state object when processing.

///

PageAsyncTask task = new PageAsyncTask (

New BeginEventHandler (Beginsubmitkeywordsasyncoperation),

New EndEventHandler (Endsubmitkeywordsasyncoperation),

New EndEventHandler (Timeoutsubmitkeywordsasyncoperation),

Null

);

RegisterAsyncTask (Task);

For most simple asynchronous processing situations, you can call the AddOnPreRenderCompleteAsync method directly,

Processing code at the start and end of a submit page request

AddOnPreRenderCompleteAsync (

New BeginEventHandler (Beginsubmitkeywordsasyncoperation),

New EndEventHandler (Endsubmitkeywordsasyncoperation)

//);

The page goes through its regular processing lifecycle until the PreRender event has just been raised. ASP.net then invokes the Begin method beginsubmitkeywordsasyncoperation using the RegisterAsyncTask registration. In this method I will submit my command to the Sokcet service and then the socket. The IAsyncResult object obtained by BeginReceive method is returned to ASP.net.

Then, my socket asynchronous callback function gets the data stream all at last, by calling the

Callback. Invoke (result);

To inform the page that the socket has finished working here.

So asp.net arouse the Endsubmitkeywordsasyncoperation method:

<summary>

The Receive method after the end of an asynchronous call (after the execution of the asynchronous operation completes, a thread is again fetched from the thread pool to request service for this page).

</summary>

<param name= "AR" ></param>

void Endsubmitkeywordsasyncoperation (IAsyncResult ar)

{

_resultdataset = _submit.getresultdataset (AR);

In this function, I can get the data stream.

After that, ASP. NET also evokes Page_PreRender events, so that the tables on the page are populated correctly in this event.

In this way, it has achieved my purpose. And after submitting to the background waiting to be activated again timeout, can also be

void Timeoutsubmitkeywordsasyncoperation (IAsyncResult ar)

To do the proper processing.

Reference resources:

1:

Asynchronous pages in ASP.net 2.0

Http://www.microsoft.com/china/msdn/library/webservices/asp.net/issuesWickedCodetoc.mspx?mfr=true

Release Date: 2006-4-19 | Renew Date: 2006-4-19

2:



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.