Introduction to Events

Events are a way to allow a class to send a signal indicating that an event of some importance has taken place. Events are most commonly used in the Windows Forms user interface, sending signals indicating that the user has clicked on a button, typed characters, moved the mouse, or any number of other events.

Events can also be used to indicate other important events that don't have anything to do with a user interface, such as indicating that an object's internal state has changed, that data in a list has changed, and so on.

Events are created from delegates using the event keyword. First, you declare the delegate:

delegate void StateChangedDelegate(object state); 

Then you declare the event as a member of a class:

public event StateChangedDelegate OnStateChanged; 

Finally, the class interested in the event can subscribe to the event in a syntax that should look familiar to you given your recent exposure to multicast delegates:

otherClass.OnStateChanged += new StateChangedDelegate(OtherClass_StateChanged); 

Now the subscribing class will be notified every time the other class's state changes.

The sample in Listing 9.4 shows a class called LongTask. This class has a method called PerformTask. This method simulates a long-running process. The class exposes an event called OnProgress that allows a subscribing class to be notified when progress on the long-running task has been made. This example also makes use of event arguments to show good design patterns that will look very familiar to you when you start working with Windows Forms.

Listing 9.4. Event Publishing and Subscribing Sample

using System; using System.Collections.Generic; using System.Text; namespace EventSample { class Program { static void Main(string[] args) {     LongTask lt = new LongTask();     lt.OnNotifyProgress += new LongTask.NotifyProgressDelegate(lt_OnNotifyProgress);     lt.PerformTask();     Console.ReadLine(); } static void lt_OnNotifyProgress(ProgressArgs pa) {     Console.WriteLine("Progress on Long Task Completed. {0}% Complete.", pa.PercentComplete); } } class LongTask { public delegate void NotifyProgressDelegate(ProgressArgs pa); public event NotifyProgressDelegate OnNotifyProgress; public void PerformTask() {     for (int i = 0; i < 10000; i++)     {         // perform some processing         // ...         // notify subscribers that progress was made         if (i % 100 == 0)         {             OnNotifyProgress(new ProgressArgs( (int) i / 100));         }     } } } class ProgressArgs { public ProgressArgs(int pctComplete) {     PercentComplete = pctComplete; } public int PercentComplete; } } 

When you run the preceding code, you will see 100 lines of text, each one reporting that another percentage point of work has been completed on the simulated long-running task.

One point about event-based programming that can't be stressed enough is that by using events, you allow a loose coupling of classes and maximize your code reuse.


A bad way to create the code in Listing 9.3 would be to write the code that performs the task directly in a Windows Form so that the code could interact directly with the user interface to indicate progress made by possibly changing the properties of a ProgressBar control. The problem that arises from that design is that the long-running task can then never be used anywhere but within that one form. If you want to perform that same task in a Web Service, a console application, a Windows Service, or even in another Windows Forms application, you will have to cut and paste code and perform some tedious tasks to get it to work.

By using events, you can then have one piece of code that is responsible for performing the task, and you could have the user interface environment subscribe to the progress event. This loose coupling allows you to reuse your code without any modification in any GUI environment.

Microsoft Visual C# 2005 Unleashed
Microsoft Visual C# 2005 Unleashed
ISBN: 0672327767
EAN: 2147483647
Year: 2004
Pages: 298 © 2008-2017.
If you may any questions please contact us: