Notes:
  1. Personally this example in the book was lacking. I've noticed that these small examples aren't really all there. And seeing it is 11pm at night, I'll leave this as it is in the book
  2. Pretty much the lesson is trying to show us that the webform will display the largest factor found by the time the webform has finished processing.
  3. Threading is an important part of large applications, it allows you to do multiple jobs at one time, rather than wait for a single process
  4. You can see that I've subscribed to two events. Both being the same event, however in my presentation I've managed them both differently. Flexibility afforded by delegates.




Front end code

    5             <asp:Label ID="lblFactor" runat="server" />

    6             <asp:TextBox id="txtLimit" Enabled="false"Text="15" runat="server" />&nbsp;



Source Code.

    1 using System;

    2 using System.Data;

    3 using System.Configuration;

    4 using System.Collections;

    5 using System.Web;

    6 using System.Web.Security;

    7 using System.Web.UI;

    8 using System.Web.UI.WebControls;

    9 using System.Web.UI.WebControls.WebParts;

   10 using System.Web.UI.HtmlControls;

   11 namespace BicNet.Projects.mcp.chapter3

   12 {

   13     public partial class EventDelegates : System.Web.UI.Page

   14     {

   15         //Declare an object

   16         MathClass MyMath;

   17 

   18         private void Page_Load(object Sender, System.EventArgs e)

   19         {

   20             if (!IsPostBack)

   21             {

   22                 //Create a new instance of MathClass with a very large seed value

   23                 MyMath = new MathClass(Int32.MaxValue - 1);

   24 

   25                 Session["MyMath"] = MyMath;

   26             }

   27             else

   28             {

   29                 //Get the stored VS variable

   30                 MyMath = (MathClass)Session["MyMath"];

   31             }

   32             //Subscribe to the OnGotFactor event.

   33             MyMath.OnGotFactor += new MathClass.OnFactorHandler(this.MyMath_OnGotFactor);

   34             MyMath.OnGotFactor +=new MathClass.OnFactorHandler(this.MyMath_OnStopCalc);

   35 

   36 

   37         }

   38 

   39         //This procedure subscribes to the MathClass OnGotFactor event

   40         void MyMath_OnGotFactor(object sender, MathEventArgs e)

   41         {

   42             Session["Factor"] = e.Factor.ToString();

   43         }

   44 

   45         //This procedure also subscribes to the mathclass OnGotFactor event.

   46         void MyMath_OnStopCalc(object sender, MathEventArgs e)

   47         {

   48             //After the limit, stop the calculations

   49             if (e.Factor > Int32.Parse(txtLimit.Text))

   50             {

   51                 //Stop the MathClass thread.

   52                 MyMath.StopCalc();

   53             }

   54         }

   55 

   56         //This procedure handles the Page's PreRender event.

   57         //It's required to display the asynchronously calculated Factor.

   58         private void Page_PreRender(object sender, EventArgs e)

   59         {

   60             //Display the current Factor

   61             if (Session["Factor"] != null)

   62                 lblFactor.Text = Session["Factor"].ToString();

   63         }

   64 

   65 }

   66 

   67     //MathClass exposes an event that fires every time a new factor is found

   68     public class MathClass

   69     {

   70         //Declares the signature of the precedures that handle this event

   71         public delegate void OnFactorHandler(Object sender, MathEventArgs e);

   72         //Exposes the event

   73         public event OnFactorHandler OnGotFactor;

   74         //internal variable used by this class

   75         protected double Seed;

   76         //This class uses a delegate from the System.Threading namespace to

   77         //invoke the CalcFactors procedure asynchronously in a

   78         //seperate thread.

   79         System.Threading.Thread CalcThread;

   80 

   81         //Class constructor sets the seed value and launches the thread

   82         public MathClass(int x)

   83         {

   84             Seed = x;

   85             //Create a new thread for the calculations

   86             CalcThread = new System.Threading.Thread(new System.Threading.ThreadStart(CalcFactors));

   87             //Launch the thread to begin Calculating factors.

   88             CalcThread.Start();

   89         }

   90 

   91         //Provides a way to stop the asynchronous processing

   92         public void StopCalc()

   93         {

   94             CalcThread.Abort();

   95         }

   96 

   97         //Finds the positive factors of the seed value and raises an event each time one is found

   98         public void CalcFactors()

   99         {

  100             //Create a MathEventArgs object as a means to return to the factors

  101             MathEventArgs e = new MathEventArgs();

  102             //declare a counter

  103             for (int i = 2; i < Math.Abs(Seed / 2); i++)

  104             {

  105                 //If there is a listener for the OnGotFactor event

  106                 //and if the number is evenly divisible by the

  107                 //counter, return the counter in the event

  108                 //arguments.

  109                 if ((OnGotFactor != null) && (Seed % i == 0))

  110                 {

  111                     e.Factor = i;

  112                     //Raise the event

  113                     OnGotFactor(this, e);

  114                 }

  115             }

  116         }

  117     }

  118 

  119     public class MathEventArgs : System.EventArgs

  120     {

  121         public int Factor;

  122     }

  123 }