topical media & game development

talk show tell print

mashup-amazon-13-13-03-SimpleAsyncWebService-Default.aspx.cs / cs



  using System;
  using System.Data;
  using System.Configuration;
  using System.Web;
  using System.Web.Security;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  using System.Web.UI.WebControls.WebParts;
  using System.Web.UI.HtmlControls;
  using localhost;
  
  public partial class _Default : System.Web.UI.Page 
  {
  
      // Two instances of the same service
      localhost.myService serviceOne;
      localhost.myService serviceTwo;
      
      // A lock to ensure shared vars are protected
      Object myLock = new Object();
      
      // Some vars to keep track of things
      string strResults;
      DateTime myStartTime;
      DateTime myEndTime; 
  
      protected void Page_Load(object sender, EventArgs e)
      {
          if (!IsPostBack)
          {
              SetStartTime();
          }
      }
      
      protected void cmdOneService_Click(object sender, EventArgs e)
      {
          SetStartTime();
          serviceOne = new localhost.myService();
          strResults += "One Service: " + serviceOne.DoWork() + "<br/><br/>";
      }
  
      void MyFunction(Object source, localhost.DoWorkCompletedEventArgs e)
      {
          // Make sure no other thread is accessing the results. 
          lock (myLock)
          {
              strResults += "Named Delegate: " + e.Result + "<br/><br/>";
          }
      }
  
      void SetStartTime()
      {
          // Get the current time
          myStartTime = DateTime.Now;
          strResults = "Page Started: " + myStartTime.ToString() + ", " + myStartTime.Millisecond.ToString() + " milliseconds<br/><br/>";
      }
  
      protected void Page_PreRenderComplete(object sender, EventArgs e)
      {        
          // Finally print out some details so you know how long stuff took
          myEndTime = DateTime.Now;
          strResults += "Page Rendering Finished: " + myEndTime.ToString() + ", " + myEndTime.Millisecond.ToString() + " milliseconds<br/><br/>";
          strResults += "Page rendering took: " + (myEndTime - myStartTime).ToString();
          lblResults.Text = strResults;
      }
  
      // Clean up
      public override void Dispose()
      {
          if (serviceOne != null) serviceOne.Dispose();
          if (serviceTwo != null) serviceTwo.Dispose();
          
          base.Dispose();
      }
  
      protected void cmdTwoSync_Click(object sender, EventArgs e)
      {
          SetStartTime();
  
          // Create two instances of the proxy
          serviceOne = new localhost.myService();
          serviceTwo = new localhost.myService();
         
          // Do some work 
          strResults += serviceOne.DoWork() + "<br/><br/>";
          strResults += serviceTwo.DoWork() + "<br/><br/>";  
      }
  
      protected void cmdTwoAsync_Click(object sender, EventArgs e)
      {
          SetStartTime();
  
          // Create two instances of the proxy
          serviceOne = new localhost.myService();
          serviceTwo = new localhost.myService();
  
          // Assign a new anonymous delegate to the 
          // completed event of the first service
          serviceOne.DoWorkCompleted += delegate(object source, DoWorkCompletedEventArgs args)
          {
              // Make sure no other thread is accessing the results. 
              lock (myLock)
              {
                  strResults += "Anonymous Delegate:" + args.Result + "<br/><br/>";
              }
          };
  
          // Register a named delegate to the completed event
          serviceTwo.DoWorkCompleted += new
                  localhost.DoWorkCompletedEventHandler(MyFunction);
  
          // Do some work 
          serviceOne.DoWorkAsync();
          serviceTwo.DoWorkAsync(); 
      }
  }
  
      


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.