The asynchronous page refresh function of ASP.NET 2.0 is really cool. I tried to use it to solve an application problem of mine today, and it really helped a lot.
My page needs to submit a query to the socket service in the background. First of all, this is an operation that may not return for a long time. Secondly, in order to get the variable length and possibly large Response data returned by the socket service, I have to use dotNET. IAsyncResult BeginReceive(byte[] buffer, int offset, int size, SocketFlags socketFlags, AsyncCallback callback, object state); method to asynchronously submit the socket command, and then iteratively call int EndReceive(IAsyncResult asyncResult); in the callback function to obtain Response data Until the data is fetched, this series of data fragments are cached in a MemoryBlock linked list.
In this case, if the ASP.NET page wants to display Response data in a table, it must be refreshed asynchronously.
ASP.NET 1.x does not inherently support asynchronous pages, but through perseverance and innovation, asynchronous pages can be generated. For more overview information, see the June 2003 MSDN® Magazine article "Use Threads and Build Asynchronous Handlers in Your Server-Side Web Code," by Fritz Onion.
But ASP.NET 2.0 greatly simplifies the way to generate asynchronous pages. First use the @ Page directive of the page to introduce the Async="true" attribute.
Secondly, when Page_Load, register the event Page_PreRender:
private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
// Execute the method when the registration event Page_PreRender is completed
// Hook PreRenderComplete event for data binding
this.PreRenderComplete += new EventHandler(Page_PreRenderComplete);
Next, I started my asynchronous operation journey in the button click event function:
/// The PageAsyncTask method is an enhanced version. In addition to the asynchronous page processing start and end methods themselves,
/// can also provide a timeout The processing method of the situation, and the status 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 directly call the AddOnPreRenderCompleteAsync method,
/// Submit the processing code at the beginning and end of the page request
//AddOnPreRenderCompleteAsync(
// new BeginEventHandler(BeginSubmitKeywordsAsyncOperation),
// new EndEventHandler(EndSubmitKeywordsAsyncOperation)
//);
The page goes through its normal processing lifecycle until just after the PreRender event is raised. Then, ASP.NET calls the Begin method BeginSubmitKeywordsAsyncOperation registered using RegisterAsyncTask. In this method, I will submit my command to the sokcet service, and then return the IasyncResult object obtained by the socket.BeginReceive method to ASP.NET.
Then, after my socket asynchronous callback function finally obtains all the data streams,
it notifies the page that the socket has completed the work
by callingcallback.Invoke(result);.
So ASP.NET calls up the EndSubmitKeywordsAsyncOperation method:
/// <summary>
/// The receiving method after the asynchronous call is completed (after the asynchronous operation is completed, a thread will be taken from the thread pool to serve the page request).
/// </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 calls the Page_PreRender event again, so that in this event, the table on the page can be filled correctly.
In this way, my purpose was achieved. Moreover, if the wait for reactivation after submitting to the background times out,
void TimeoutSubmitKeywordsAsyncOperation(IAsyncResult ar)
can alsobe used to handle it appropriately.
Reference resources:
1:
Asynchronous pages in ASP.NET 2.0
http://www.microsoft.com/china/msdn/library/webservices/asp.net/issuesWickedCodetoc.mspx?mfr=true
http://www.cnblogs. com/zhengyun_ustc/archive/2006/08/08/asp_net_2_0_pageasynctask.html