Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Daniel Solis - Illustrated C# 2010 - 2010.pdf
Скачиваний:
20
Добавлен:
11.06.2015
Размер:
11.23 Mб
Скачать

CHAPTER 16 EVENTS

Subscribing to an Event

To add an event handler to an event, the handler must have the same return type and signature as the event’s delegate.

Use the += operator to add an event handler to an event, as shown in the following code.

The method can be any of the following:

An instance method

A static method

An anonymous method

A lambda expression

For example, the following code adds three methods to event Elapsed. The first is an instance method using the method form. The second is a static method using the method form. The third is an instance method using the delegate form.

Class instance

Instance method

 

 

 

mc.Elapsed += ca.TimerHandlerA;

// Method reference form

mc.Elapsed += ClassB.TimerHandlerB;

// Method reference form

 

 

 

 

Event member

Static method

 

mc.Elapsed += new EventHandler(cc.TimerHandlerC);

// Delegate form

Just as with delegates, you can use anonymous methods and lambda expressions to add event handlers. For example, the following code first uses a lambda expression and then uses an anonymous method.

mc.Elapsed += (source, args) =>

// Lambda expression

{

 

Console.WriteLine("Lambda expression.");

 

};

 

mc.Elapsed += delegate(object source, EventArgs args)

// Anonymous method

{

 

Console.WriteLine("Anonymous method.");

 

};

 

398

CHAPTER 16 EVENTS

The following program uses the MyTimerClass class declared in the previous section. The code performs the following:

It registers two event handlers from two different class instances.

After registering the event handlers, it sleeps for two seconds. During that time, the timer class raises the event two times, and both event handlers are executed each time.

public class MyTimerClass { ... }

 

 

 

class ClassA

 

 

 

{

 

 

 

public void TimerHandlerA(object source, EventArgs args)

// Event handler

{

 

 

 

Console.WriteLine("Class A handler called");

 

 

}

 

 

 

}

 

 

 

class ClassB

 

 

 

{

 

 

 

public static void TimerHandlerB(object source, EventArgs args)

// Static

{

 

 

 

Console.WriteLine("Class B handler called");

 

 

}

 

 

 

}

 

 

 

class Program

 

 

 

{

 

 

 

static void Main( )

 

 

 

{

 

 

 

ClassA ca = new ClassA();

// Create the class object.

MyTimerClass mc = new MyTimerClass();

// Create the timer object.

mc.Elapsed += ca.TimerHandlerA;

// Add handler A -- instance.

mc.Elapsed += ClassB.TimerHandlerB;

// Add handler B -- static.

Thread.Sleep(2250);

}

}

When supplied with the code for MyTimerClass, this code produces the following output:

Class A handler called

Class B handler called

Class A handler called

Class B handler called

399

CHAPTER 16 EVENTS

Removing Event Handlers

When you’re done with an event handler, you should remove it from the event, to allow the garbage collector to free up that memory. You remove an event handler from an event by using the -= operator, as shown here:

mc.Elapsed -= ca.TimerHandlerA;

// Remove handler A.

For example, the following code removes the event handler for ClassB after the first two times the event is raised and then lets the program run for another two seconds.

...

 

 

mc.Elapsed += ca.TimerHandlerA;

// Add instance handler A.

mc.Elapsed += ClassB.TimerHandlerB;

// Add static

handler B.

Thread.Sleep(2250);

// Sleep more

than 2 seconds.

mc.Elapsed -= ClassB.TimerHandlerB;

// Remove static handler B.

Console.WriteLine("Class B event handler removed");

 

Thread.Sleep(2250);

// Sleep more

than 2 seconds.

This code produces the following output. The first four lines are the result of both handlers being called twice, in the first two seconds. After the handler for ClassB is removed, only the handler for the instance of ClassA is called, during the last two seconds.

Class A handler called

Class B handler called

Class A handler called

Class B handler called

Class B event handler removed

Class A handler called

Class A handler called

400

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]