Console.WriteLine('File renamed: ' + e.FullPath);

}

static void fileWatcher_Deleted(object sender, FileSystemEventArgs e) {

 Console.WriteLine('File deleted: ' + e.FullPath);

}

To test the program, you can create a new text file in C:drive, make some changes to its content, rename it, and then delete it. The output window will look like Figure 7-9.

Figure 7-9

Implementing Events

So far you have been subscribing to events by writing event handlers. Now you will implement events in your own class. For this example, you create a class called AlarmClock. AlarmClock allows you to set a particular date and time so that you can be notified (through an event) when the time is up. For this purpose, you use the Timer class.

First, define the AlarmClock class as follows:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Timers;

class AlarmClock {

}

Declare a Timer variable and define the AlarmTime property to allow users of this class to set a date and time:

class AlarmClock {

 Timer t;

 public DateTime AlarmTime { get; set; }

}

Next, define the Start() method so that users can start the monitoring by turning on the Timer object:

class AlarmClock {

 //...

 public void Start() {

  t.Start();

 }

}

Next, define a public event member in the AlarmClock class:

public event EventHandler TimesUp;

The EventHandler is a predefined delegate, and this statement defines TimesUp as an event for your class.

Define a protected virtual method in the AlarmClock class that will be used internally by your class to raise the TimesUp event:

protected virtual void onTimesUp(EventArgs e) {

 if (TimesUp != null) TimesUp(this, e);

}

The EventArgs class is the base class for classes that contain event data. This class does not pass any data back to an event handler.

The next section explains how you can create another class that derives from this EventArgs base class to pass back information to an event handler.

Define the constructor for the AlarmClock class so that the Timer object (t) will fire its Elapsed event every 100 milliseconds. In addition, wire the Elapsed event with an event handler. The event handler will check the current time against the time set by the user of the class. If the time equals or exceeds the user's set time, the event handler calls the onTimesUp() method that you defined in the previous step:

public AlarmClock() {

 t = new Timer(100);

 t.Elapsed += new ElapsedEventHandler(t_Elapsed);

}

void t_Elapsed(object sender, ElapsedEventArgs e) {

 if (DateTime.Now >= this.AlarmTime) {

  onTimesUp(new EventArgs());

  t.Stop();

 }

}

That's it! The entire AlarmClock class is:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Timers;

class AlarmClock {

 Timer t;

 public DateTime AlarmTime { get; set; }

 public void Start() {

  t.Start();

 }

 public AlarmClock() {

  t = new Timer(100);

  t.Elapsed += new ElapsedEventHandler(t_Elapsed);

 }

 void t_Elapsed(object sender, ElapsedEventArgs e) {

  if (DateTime.Now >= this.AlarmTime) {

   onTimesUp(new EventArgs());

   t.Stop();

  }

 }

 public event EventHandler TimesUp;

 protected virtual void onTimesUp(EventArgs e) {

  if (TimesUp != null) TimesUp(this, e);

 }

}

Вы читаете C# 2008 Programmer's Reference
Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату