Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Лаба №1 / books / csharp_ebook.pdf
Скачиваний:
77
Добавлен:
03.03.2016
Размер:
3.69 Mб
Скачать

Programmers Heaven: C# School

for(int i=0; i<5; i++)

{

Timer(this, null);

Thread.Sleep(1000);

}

}

}

}

Note that we have also included the System.Threading namespace at the start of the program, as we are using its Thread class in our code. The output of the program is:

Received a clock tick event!

Received a clock tick event!

Received a clock tick event!

Received a clock tick event!

Received a clock tick event!

Press any key to continue

Each message is printed with a delay of one second and five messages are printed in total.

Multicast events

Since events are implemented as multicast delegates in C#, we can subscribe multiple event handlers to a single event. For example, consider this revised Test class:

class Test

{

static void Main()

{

ClockTimer clockTimer = new ClockTimer(); clockTimer.Timer += new TimerEvent(OnClockTick); clockTimer.Timer += new TimerEvent(OnClockTick2); clockTimer.Start();

}

public static void OnClockTick(object sender, EventArgs e)

{

Console.WriteLine("Received a clock tick event!");

}

public static void OnClockTick2(object sender, EventArgs e)

185

Programmers Heaven: C# School

{

Console.WriteLine("Received a clock tick event in OnClockTick2!");

}

}

Here we have introduced another event handler, 'OnClockTick2', and have subscribed it also to the Timer event in the Main() method using the '+=' operator. The output of this program is:

Received a clock

tick event!

Received a clock

tick event in OnClockTick2!

Received a clock

tick event!

Received a clock

tick event in OnClockTick2!

Received a clock

tick event!

Received a clock

tick event in OnClockTick2!

Received a clock

tick event!

Received a clock

tick event in OnClockTick2!

Received a clock

tick event!

Received a clock

tick event in OnClockTick2!

Press any key to

continue

 

 

As can be seen in the output above, now both the OnClockTick() and OnClockTick2() are invoked each time the event is raised.

Passing some data with the Event: Sub-classing System.EventArgs

Finally, we can pass some additional information while raising an event. For this, we need to perform the following three steps:

1.Define a class that inherits from System.EventArgs

2.Encapsulate the data to be passed with the event within this class (preferably using properties)

3.Create an instance of this class in the event generator class and pass it with the event

Let's now change our previous Clock Timer example so that the event raised also contains the sequence number of the clock ticks. First we need to define a new class 'ClockTimerArgs', which inherits from the System.EventArgs class.

public class ClockTimerArgs : EventArgs

{

private int tickCount;

public ClockTimerArgs(int tickCount)

{

this.tickCount = tickCount;

186

Programmers Heaven: C# School

}

public int TickCount

{

get { return tickCount; }

}

}

The ClockTimerArgs class contains a private variable named 'tickCount' to hold the current tick number. This value is passed to the object through a public constructor and is accessible to the event handler through the public property. Next we need to change the delegate definition for the event to:

public delegate void TimerEvent(object sender, ClockTimerArgs e);

The argument type in the delegate is changed from EventArgs to ClockTimerArgs so that the publisher (event generator) can pass this particular type of arguments to the subscriber (event handler). The event generator class is defined as:

class ClockTimer

{

public event TimerEvent Timer;

public void Start()

{

for(int i=0; i<5; i++)

{

Timer(this, new ClockTimerArgs(i+1));

Thread.Sleep(1000);

}

}

}

The only change in this class is that instead of passing null as the second argument, we are passing a new object of the ClockTimerArgs type with the sequence number of the current clock tick. Finally, the event handler is written:

public static void OnClockTick(object sender, ClockTimerArgs e)

{

Console.WriteLine("Received a clock tick event. This is clock tick number {0}", e.TickCount);

}

Here we have simply printed the clock tick number using the ClockTimerArgs' TickCount Property. The complete source code is shown below.

187

Programmers Heaven: C# School

using System;

using System.Threading;

namespace CSharpSchool

{

class Test

{

static void Main()

{

ClockTimer clockTimer = new ClockTimer(); clockTimer.Timer += new TimerEvent(OnClockTick); clockTimer.Start();

}

public static void OnClockTick(object sender, ClockTimerArgs e)

{

Console.WriteLine("Received a clock tick event. This is clock tick number {0}", e.TickCount);

}

}

public class ClockTimerArgs : EventArgs

{

private int tickCount;

public ClockTimerArgs(int tickCount)

{

this.tickCount = tickCount;

}

public int TickCount

{

get { return tickCount; }

}

}

public delegate void TimerEvent(object sender, ClockTimerArgs e);

class ClockTimer

{

public event TimerEvent Timer; public void Start()

{

for(int i=0; i<5; i++)

{

188

Programmers Heaven: C# School

Timer(this, new ClockTimerArgs(i+1));

Thread.Sleep(1000);

}

}

}

}

When the above program is compiled and executed, we will see the following output:

Received a clock tick event. This is clock tick number 1

Received a clock tick event. This is clock tick number 2

Received a clock tick event. This is clock tick number 3

Received a clock tick event. This is clock tick number 4

Received a clock tick event. This is clock tick number 5

Press any key to continue

As the output of the program illustrates, now we are also receiving the clock tick number along with each event.

189

Соседние файлы в папке books