Online Book Reader

Home Category

Programming Microsoft ASP.NET 4 - Dino Esposito [109]

By Root 5623 0
ar)

{

// This code will be called on a(nother) pooled thread

using (var response = req.EndGetResponse(ar))

{

String text;

using (var reader = new StreamReader(response.GetResponseStream()))

{

text = reader.ReadToEnd();

}

// Process the RSS data

rssData = ProcessFeed(text);

}

// Trace

Trace.Warn("End async: Thread=" +

Thread.CurrentThread.ManagedThreadId.ToString());

// The page is updated using an ASP-style code block in the ASPX

// source that displays the contents of the rssData variable

}

String ProcessFeed(String feed)

{

// Build the page output from the XML input

...

}

}

As you can see, such an asynchronous page differs from a standard one only for the aforementioned elements—the Async directive attribute and the pair of asynchronous event handlers. Figure 5-4 shows the sample page in action.

Figure 5-4. A sample asynchronous page downloading links from a blog.

It would also be interesting to take a look at the messages traced by the page. Figure 5-5 provides visual clues of it. The Begin and End stages are served by different threads and take place at different times.

Note the time elapsed between the Exit BeginTask and Enter EndTask stages. It is much longer than intervals between any other two consecutive operations. It’s in that interval that the lengthy operation—in this case, downloading and processing the RSS feed—took place. The interval also includes the time spent to pick up another thread from the pool to serve the second part of the original request.

Figure 5-5. The traced request details clearly show the two steps needed to process a request asynchronously.

The RegisterAsyncTask Method


The AddOnPreRenderCompleteAsync method is not the only tool you have to register an asynchronous task. The RegisterAsyncTask method is, in most cases, an even better solution. RegisterAsyncTask is a void method and accepts a PageAsyncTask object. As the name suggests, the PageAsyncTask class represents a task to execute asynchronously.

The following code shows how to rework the sample page that reads some RSS feed and make it use the RegisterAsyncTask method:

void Page_Load (object sender, EventArgs e)

{

PageAsyncTask task = new PageAsyncTask(

new BeginEventHandler(BeginTask),

new EndEventHandler(EndTask),

null,

null);

RegisterAsyncTask(task);

}

The constructor accepts up to five parameters, as shown in the following code:

public PageAsyncTask(

BeginEventHandler beginHandler,

EndEventHandler endHandler,

EndEventHandler timeoutHandler,

object state,

bool executeInParallel)

The beginHandler and endHandler parameters have the same prototype as the corresponding handlers you use for the AddOnPreRenderCompleteAsync method. Compared to the AddOnPreRenderCompleteAsync method, PageAsyncTask lets you specify a timeout function and an optional flag to enable multiple registered tasks to execute in parallel.

The timeout delegate indicates the method that will get called if the task is not completed within the asynchronous timeout interval. By default, an asynchronous task times out if it’s not completed within 45 seconds. You can indicate a different timeout in either the configuration file or the @Page directive. Here’s what you need if you opt for the web.config file:

The @Page directive contains an integer AsyncTimeout attribute that you set to the desired number of seconds.

Just as with the AddOnPreRenderCompleteAsync method, you can pass some state to the delegates performing the task. The state parameter can be any object.

The execution of all tasks registered is automatically started by the Page class code just before the async point is reached. However, by placing a call to the ExecuteRegisteredAsyncTasks method on the Page class, you can take control of this aspect.

Choosing the Right Approach


When should you use AddOnPreRenderCompleteAsync, and when is RegisterAsyncTask a better option? Functionally speaking, the two approaches are nearly identical. In both cases, the execution

Return Main Page Previous Page Next Page

®Online Book Reader