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

CHAPTER 16 EVENTS

The MyTimerClass Code

Now that you’ve seen all five components of code that need to be implemented to use an event, I can show you the full MyTimerClass class that the code has been using.

Most things about the class have been pretty clear—it has an event called Elapsed that can be subscribed to and a method called OnOneSecond that is called every second and raises the event. The one question remaining about it is, “What causes OnOneSecond to be called every second?”

The answer is that I’ve created method OnOneSecond and subscribed it as an event handler to an event in a class called Timer, in the System.Timers namespace. The event in Timer is raised every 1,000 milliseconds and calls event handler OnOneSecond, which in turn raises event Elapsed in class MyTimerClass. Figure 16-5 shows the structure of the code.

Figure 16-5. The code structure of MyTimerClass

The Timer class is a useful tool, so I’ll mention a little more about it. First, it has a public event called Elapsed. If that sounds familiar, it’s because I named the event in MyTimerClass after it. The names have no other connection than that. I could have named the event in MyTimerClass anything.

One of the properties of Timer is Interval, which is of type double, and specifies the number of milliseconds between raising the event. The other property the code uses is Enabled, which is of type bool, and starts and stops the timer.

406

CHAPTER 16 EVENTS

The actual code is the following. The only things I haven’t shown previously are the private timer field, called MyPrivateTimer, and the constructor for the class. The constructor does the work of setting up the internal timer and attaching it to event handler OnOneSecond.

public class MyTimerClass

{

public event EventHandler Elapsed;

private void OnOneSecond(object source, EventArgs args)

{

if (Elapsed != null) Elapsed(source, args);

}

 

 

//------------

 

 

private System.Timers.Timer MyPrivateTimer;

//

Private timer

public MyTimerClass()

//

Constructor

{

 

 

MyPrivateTimer = new System.Timers.Timer(); // Create the private timer.

//The following statement sets our OnOneSecond method above as an event

//handler to the Elapsed event of class Timer. It is completely

//unrelated to our event Elapsed, declared above.

MyPrivateTimer.Elapsed += OnOneSecond;

// Attach our event handler.

//Property Interval is of type double, and specifies the number of

//milliseconds between when its event is raised.

MyPrivateTimer.Interval =

1000;

//

1 second interval.

// Property Enabled is

of

type

bool, and turns

the timer on and off.

MyPrivateTimer.Enabled

= true;

// Start the timer.

}

}

407

CHAPTER 16 EVENTS

Event Accessors

The last topic to cover in this chapter is event accessors. I mentioned earlier that the += and -= operators were the only operators allowed for an event. These operators have the well-defined behavior that you’ve seen so far in this chapter.

You can, however, change these operators’ behavior and have the event perform whatever custom code you like when they are used. You can do this by defining event accessors for the event.

There are two accessors: add and remove.

The declaration of an event with accessors looks similar to the declaration of a property.

The following example shows the form of an event declaration with accessors. Both accessors have an implicit value parameter called value that takes a reference to either an instance method or a static method.

public event EventHandler Elapsed

 

{

 

add

 

{

 

...

// Code to implement the =+ operator

}

 

remove

 

{

 

...

// Code to implement the -= operator

}

 

}

 

When event accessors are declared, the event does not contain an embedded delegate object. You must implement your own storage mechanism for storing and removing the methods registered with the event.

The event accessors act as void methods, meaning that they cannot use return statements that return a value.

408

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