how can we use AsynCallback method of web service in asp.net

Posted by sameer on Stack Overflow See other posts from Stack Overflow or by sameer
Published on 2010-05-23T15:35:34Z Indexed on 2010/05/23 15:41 UTC
Read the original article Hit count: 327

Filed under:
|

Hi All,

I was going through the proxy class which is generated using wsdl.exe found the asyncmethod like BeginAsynXXX() and EndAsyncXXX(). i understood how to utilize them on Windows application but i was wondering how can we use them in Web Application built using asp.net

here is the code for web service client build as windows application.can any tell me how we can do this with web application.

using System;
using System.Runtime.Remoting.Messaging;
using MyFactorize;

   class TestCallback
 {           
  public static void Main(){
        long factorizableNum = 12345;
        PrimeFactorizer pf = new PrimeFactorizer();

        //Instantiate an AsyncCallback delegate to use as a parameter
        //in the BeginFactorize method.
        AsyncCallback cb = new AsyncCallback(TestCallback.FactorizeCallback);

      // Begin the Async call to Factorize, passing in our
      // AsyncCalback delegate and a reference
      // to our instance of PrimeFactorizer.
        IAsyncResult ar = pf.BeginFactorize(factorizableNum, cb, pf);

        // Keep track of the time it takes to complete the async call
        // as the call proceeds.
     int start = DateTime.Now.Second;
     int currentSecond = start;
     while (ar.IsCompleted == false){
        if (currentSecond < DateTime.Now.Second) {
              currentSecond = DateTime.Now.Second;
              Console.WriteLine("Seconds Elapsed..." + (currentSecond - start).ToString() );
        }
     }
     // Once the call has completed, you need a method to ensure the
     // thread executing this Main function 
     // doesn't complete prior to the call-back function completing.
     Console.Write("Press Enter to quit");
     int quitchar = Console.Read();
  }
  // Set up a call-back function that is invoked by the proxy class
  // when the asynchronous operation completes.
  public static void FactorizeCallback(IAsyncResult ar)
  {
      // You passed in our instance of PrimeFactorizer in the third
      // parameter to BeginFactorize, which is accessible in the
      // AsyncState property.
      PrimeFactorizer pf = (PrimeFactorizer) ar.AsyncState;
      long[] results;

      // Get the completed results.
        results = pf.EndFactorize(ar);

      //Output the results.
        Console.Write("12345 factors into: ");
        int j;
        for (j = 0; j<results.Length;j++){
              if (j == results.Length - 1)
                  Console.WriteLine(results[j]);
              else 
                  Console.Write(results[j] + ", ");
        }
  }

}

© Stack Overflow or respective owner

Related posts about web-services

Related posts about web