Working with Delegates and Events


In this section we're going to start a project that will help you better understand the concept of using asynchronous delegates. Just what is an asynchronous delegate? We're going to answer that throughout the chapter, but for now let's just say that asynchronous delegates let your Web page do more than one thing at the same time.

For example, suppose you need to read a list of names and a list of states from two different database tables. The two pieces of data may be totally independent, so rather than reading from one table, waiting until we're done reading, and then reading from a different table, it may be beneficial to read from both tables at the same time. This can easily be done with asynchronous delegates.

One way in which a program can do multiple things at the same time is to use threading. A thread is a list of operations that must be done in order. Every application begins with one thread, and all operations occur sequentially. If an application wants to do multiple things, it needs to create another thread. The operating system then switches between the threads allowing one thread to do some work while pausing the other threads, then pausing the first thread and allow others to do work. If the machine has more than one processor, the OS can assign each thread to a different processor so that operations can happen at the same time.

One of the simplest ways of using multithreading in C# is to use asynchronous delegates.

For our sample application, I'm going to explain the mechanism for running multiple tasks by having your page perform three tasks concurrently. The tasks aren't anything special; they will just pause execution for a few seconds. Then we're going to first execute the tasks in order, one after the other, and then concurrently, and compare the execution times.

To create a test project for this chapter:

  1. Launch Visual Studio .NET. (Start > All Programs > Microsoft Visual Studio .NET > Microsoft Visual Studio .NET).

  2. Select File > New > Project to bring up the New Project dialog.

  3. Under Project Types on the left side of the New Project window, click the Visual C# projects folder.

  4. Select the ASP.NET Web Application icon and change the name of the application to delegatesproject ( Figure 10.1 ).

    Figure 10.1. For this sample application we're going to create an ASP.NET Web Application. Name your application delegatesproject.

    graphics/10fig01.gif

  5. Visual Studio will create a new project and open WebForm1.aspx.

  6. Change the form's name to dispatcher.aspx by choosing View > Solution Explorer from the top menu bar.

  7. Right-click on WebForm1.aspx and choose Properties. In the property grid below change the FileName property from WebForm1.aspx to dispatcher.aspx ( Figure 10.2 ).

    Figure 10.2. This application will only have a single form called dispatcher.aspx.

    graphics/10fig02.gif

  8. Create a form that looks like the form in Figure 10.3 . This is a lot of work to do by hand, so you may want to enter the HTML directly into the editor. Figure 10.4 shows the HTML necessary to create the form. To enter it directly, click the HTML button under the editor's window. As an alternative you could download the skeleton file for this project (see Tips on next page).

    Figure 10.3. The dispatcher.aspx form is a simple form with two labels and two buttons . The first button will execute two tasks synchronouslythat is, it will execute Task1, wait for it to be done, then execute Task2. The second button will execute the same tasks asynchronously (both at the same time).

    graphics/10fig03.gif

    Figure 10.4 If you are trying to recreate the form by hand you may want to use this HTML for reference.
     <%@ Page language="c#"   Codebehind="dispatcher.aspx.cs"   AutoEventWireup="false"   Inherits="delegatesproject.WebForm1" %> <HTML>    <HEAD>       <title>WebForm1</title>    </HEAD>    <body MS_POSITIONING="GridLayout">    <form id="Form1" method="post"            runat="server">    <  asp:Button  id="btnSync" style="Z-            INDEX: 100; LEFT: 30px;            POSITION: absolute; TOP: 52px"            runat="server"            Text="Perform Tasks            Synchronously"            Width="275px">            </asp:Button>    <  asp:Button  id="btnAsync" style="Z-            INDEX: 104; LEFT: 30px;            POSITION: absolute; TOP: 89px"            runat="server"            Text="Perform Tasks            Asynchronously"            Width="275px">            </asp:Button>    <  asp:Label  id="lblTimeElapsed"            style="Z-INDEX: 102; LEFT:            30px; POSITION: absolute; TOP:            23px" runat="server">            Time Elapsed:            </asp:Label>    <  asp:Label  id="lblTime" style="Z-            INDEX: 103; LEFT: 125px;            POSITION: absolute; TOP: 23px"            runat="server">            </asp:Label>    </form>    </body> </HTML> 

graphics/tick.gif Tips

  • As with the other projects in this book, building the project isn't necessary for learning the concepts in this chapter.

  • Skeletons for each project can be downloaded from Peachpit's Web site, http://www.peachpit.com/vqs/csharp.




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