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() + "

"; } void MyFunction(Object source, localhost.DoWorkCompletedEventArgs e) { // Make sure no other thread is accessing the results. lock (myLock) { strResults += "Named Delegate: " + e.Result + "

"; } } void SetStartTime() { // Get the current time myStartTime = DateTime.Now; strResults = "Page Started: " + myStartTime.ToString() + ", " + myStartTime.Millisecond.ToString() + " milliseconds

"; } 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

"; 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() + "

"; strResults += serviceTwo.DoWork() + "

"; } 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 + "

"; } }; // Register a named delegate to the completed event serviceTwo.DoWorkCompleted += new localhost.DoWorkCompletedEventHandler(MyFunction); // Do some work serviceOne.DoWorkAsync(); serviceTwo.DoWorkAsync(); } }