Finishing the Sample Application


At this point, you know enough to finish the sample application, which will show you the difference timewise between executing two functions synchronously and executing them asynchronously.

To measure the time difference, we're going to make use of the classes System.DateTime and System.TimeSpan. System.DateTime.Now reports the current date and time. We can record the date and time before and after the functions complete and print out the difference in seconds between the synchronous method and the asynchronous one.

The only other thing we need is a way to execute tasks that actually take a noticeable amount of time to execute. To do so we'll make use of a function called System.Threading.Thread.Sleep. This function enables us to stop execution for a specified amount of time, expressed in milliseconds .

To complete the sample application:

  1. From the Solution Explorer window, rightclick on dispatcher.aspx and choose View Code from the pop-up menu. You should see the code for the WebForm1 class inside the delegateproject namespace.

  2. Add a class called Tasks. The Tasks class should have two functions, Task1 and Task2. These two functions should do nothing but pause for 5 seconds (5000 milliseconds) ( Figure 10.22 ).

    Figure 10.22 The Tasks class has two functions, Task1 and Task2. One of them is an instance function, the other is a static function. This was done to illustrate that the same delegate type can invoke either an instance method or a static method. Both Task1 and Task2 pause execution for five seconds.
     namespace delegatesproject {  class Tasks  {    public bool Task1(string desc)    {       System.Threading.Thread.       Sleep(5000);       return true;     }    public static bool Task2(string desc)    {       System.Threading.Thread.       Sleep(5000);       return true;    }  }  class WebForm1 : System.Web.UI.Page //more code here 
  3. Add a definition for a delegate that can invoke either Task1 or Task2 ( Figure 10.23 ).

    Figure 10.23 The TaskDel delegate can store functions that have one string input parameter and one Boolean output parameter. So Task1 and Task2 are both valid functions to store in the delegate.
     namespace delegatesproject { class Tasks {    public bool Task1(string desc)    {       System.Threading.Thread.       Sleep(5000);       return true;    }    public static bool Task2(string desc)    {       System.Threading.Thread.       Sleep(5000);       return true;    } }  delegate bool TaskDel(string desc);  class WebForm1 : System.Web.UI.Page //more code here 
  4. From the Solution Explorer window, right-click on dispatcher.aspx and choose View Designer from the popup menu.

  5. Double-click on the Perform Tasks Synchronously button to have the wizard add the stub for the Click event of the button to the code.

  6. Repeat steps 4 and 5, but this time double-click on the Perform Tasks Asynchronously button.

  7. Add the code in Figure 10.24 to the btnSync_Click function. This code first records the current time, then calls the functions Task1 and Task2 in order, and records the ending time. Finally, it displays in seconds the time it took to execute both tasks.

    Figure 10.24 The synchronous version of the function calls both Task1 and Task2 in the traditional fashion, without using delegates.
     class WebForm1 : System.Web.UI.Page {    //more code here  private void btnSync_Click(   object sender, System.EventArgs e)  {       Tasks tsks = new Tasks();       System.DateTime dt1 =       System.DateTime.Now;       tsks.Task1("Calling Task1...");       Tasks.Task2("Calling Task2...");       System.DateTime dt2 =       System.DateTime.Now;       System.TimeSpan tm = dt2 - dt1;       lblTime.Text =       tm.TotalSeconds.ToString();    } } 
  8. Add the code in Figure 10.25 to the btnAsync_Click function. This code creates delegates for each Task function and then invokes the function asynchronously. Both functions will execute in different threads. The function then displays the time it took to execute both functions in seconds.

    Figure 10.25 The asynchronous version of the function uses delegates to invoke both functions asynchronously. Both functions record the time before and after execution, then print out the difference in seconds.
     class WebForm1 : System.Web.UI.Page {    //more code here    private void btnSync_Click(object sender, System.EventArgs e)    {       //code entered previously    }  private void btnAsync_Click(   object sender, System.EventArgs e)  {       Tasks tsks = new Tasks();       TaskDel del1 = new       TaskDel(tsks.Task1);       TaskDel del2 = new       TaskDel(Tasks.Task2);       System.DateTime dt1 =       System.DateTime.Now;       IAsyncResult ar1 =       del1.BeginInvoke("Calling Task1...",null,null);       IAsyncResult ar2 =       del2.BeginInvoke("Calling Task2...",null,null);       System.Threading.WaitHandle[]       handles = new       System.Threading.WaitHandle[]       {ar1.AsyncWaitHandle ,       ar2.AsyncWaitHandle };       System.Threading.WaitHandle.       WaitAll(handles);       System.DateTime dt2 =       System.DateTime.Now;       System.TimeSpan tm = dt2 - dt1;       lblTime.Text =       tm.TotalSeconds.ToString();    } } 

graphics/tick.gif Tips

  • The DateTime function overloads the operator to return a TimeSpan object. When you subtract one date from another, the time difference is reported in the TimeSpan object. You can then use the TimeSpan's TotalSeconds function to report the time difference in seconds.

  • Execute the program and click on each button. You should see that in the synchronous case, the tasks complete in about ten seconds. In the asynchronous case you should see both tasks completing in about five seconds. If you have a computer with multiple processors (lucky you) then the tasks can really happen at the same time. With a single processor, the OS gives each thread some time to execute and continues to switch between each thread.




C#
C# & VB.NET Conversion Pocket Reference
ISBN: 0596003196
EAN: 2147483647
Year: 2003
Pages: 198
Authors: Jose Mojica

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net