Infolink

 

Search This Blog

Dec 15, 2013

Asynchronous Methods PART-2

Everybody would agree that asynchronously-served requests are important for the performance of potentially-lengthy operations. Taking this point to the limit, Windows 8 is highly dependent upon asynchronous operations and that is the preferred way of coding in the newest platform.

So what’s the real benefit of implementing asynchronous requests?

The benefits of asynchronous requests are entirely for the server environment. In ASP.NET, async HTTP handlers keep the server runtime much more responsive by guaranteeing that a larger number of threads are available at any time to serve requests. In the end, it won’t so much be the requests for long-running tasks that benefit from async handlers, but all other requests, for images, scripts, and other static and ASPX pages. In this way, the site remains highly responsive for all users and long-running requests will still take their time to complete. 

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class helloService : System.Web.Services.WebService {

    public helloService () {

        //Uncomment the following line if using designed components
        //InitializeComponent();
    }

    [WebMethod]
    public IAsyncResult BeginHelloWorld(IAsyncResult a, IAsyncResult b)
    {
        return a;
     } 
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ServiceReference1;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        helloService h = new helloService();
        IAsyncResult ar1 = h.BeginHelloWorld(null, null);
        IAsyncResult ar2 = h.BeginHelloWorld(null,null);
       
        TextBox1.Text = helloService.EndHelloWorld(ar1);

        TextBox2.Text = helloService.EndHelloWorld(ar2);
        PageAsyncTask task1;
     }
}


----------------------------------------------------------------------------------------

protected void Page_Load(object sender, EventArgs e)
     {
         PageAsyncTask task1;

         PageAsyncTask task2;

         bool executeInParallel = true;    

         task1 = new PageAsyncTask
             (

                delegate(Object source, EventArgs ea, AsyncCallback callback, Object state)

                    { return helloService.BeginHelloWorld(null, callback, state); },

                delegate(IAsyncResult ar)

                    { TextBox1.Text = helloService.EndHelloWorld(ar); },

                // dont' need a TimeOut handler, or state object

                null, null, executeInParallel
            );

        task2 = new PageAsyncTask
            (

                delegate(Object source, EventArgs ea, AsyncCallback callback, Object state)
                    { return helloService.BeginHelloWorld(null, callback, state); },

                delegate(IAsyncResult ar)

                    { TextBox2.Text = helloService.EndHelloWorld(ar); },

               null, null, executeInParallel
            );         
        RegisterAsyncTask(task1);
        RegisterAsyncTask(task2);      
    }
I think Async pages are a great addition to 2.0 when used in the right situations. You can easily control the parallelizing of tasks, easily handle timeouts, and thread management is superior compared to the simple approach in the first code snippet.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...