When you use the "Async" methods of a WebService in .NET 2.0 the ASP .NET 2.0 page will delay rendering until completion of the Async method call.
This can be very handy, but what if you do not want to delay the rendering of the page, because you are not interested in the result.
Then you must use another method: Delegates, BeginInvoke and Synchronous WebService call
Yes, this is right "Synchronous WebService call"

    /// The delegate, used for asynchronous processing.
    public delegate TOutput SomeMethod<TOutput>();

    /// <summary>
    /// Click Event, user clicked on the LinkButtonTest.
    ///
    /// History
    /// [RVL 01-feb-2008] Creation
    /// </summary>
    protected void LinkButtonTest_Click(object sender, EventArgs e)
    {
     this.AsnchronousProgramming();
    }

    /// <summary>
    /// Test the asynchronous execution of a method via a delegate and BeginInvoke in .NET 2.0
    ///
    /// History
    /// [RVL 01-feb-2008] Creation
    /// </summary>
    private void AsnchronousProgramming()
    {
     Debug.WriteLine("Start the asynchronous processen");
     SomeMethod<int> method = this.SomeCalculation;
    
     method.BeginInvoke(EndSomeCalculation, method);

     Debug.WriteLine("We can do some processing here");
    }

    /// <summary>
    /// The calculation is done.
    ///
    /// History
    /// [RVL 01-feb-2008] Creation
    /// </summary>
    public void EndSomeCalculation(IAsyncResult result)
    {
     Debug.WriteLine("SomeCalculatoin completed");
     SomeMethod<int> method = result.AsyncState as SomeMethod<int>;

     Debug.WriteLine("Get result");
     int i = method.EndInvoke(result);
    
     Debug.WriteLine("Asynchronous processing completed");
    }

/// <summary>
    /// Some calculation to demonstrate the asynchronous execution.
    ///
    /// History
    /// [RVL 01-feb-2008] Creation
    /// </summary>
    private int SomeCalculation()
    {
     int result = 1;

     Debug.WriteLine("Simulate syncronous webservice call or lang during processing (10s)");
     Thread.Sleep(10000);

     Debug.WriteLine("Calcula
te the result"
);
     result += 1;

     return result;
    }

See also: Matthew Cochran, http://www.c-sharpcorner.com/UploadFile/rmcochran/multithreadedasync05112007061031AM/multithreadedasync.aspx

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.