Console.WriteLine("File created: " + e.FullPath);
}
static void fileWatcher_Changed(object sender, FileSystemEventArgs e) {
Console.WriteLine("File changed: " + e.FullPath);
}
static void fileWatcher_Renamed(object sender, RenamedEventArgs e) {
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
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);
}
}
To use the AlarmClock
class, you first create an instance of the AlarmClock
class and then set the time for the alarm by using the AlarmTime
property. You then wire the TimesUp
event with an event handler so that you can print a message when the set time is up:
class Program {
static void Main(string[] args) {
AlarmClock c = new AlarmClock() {
//---alarm to sound off at 16 May 08, 9.50am---
AlarmTime = new DateTime(2008, 5, 16, 09, 50, 0, 0),
};
c.Start();
c.TimesUp += new EventHandler(c_TimesUp);
Console.ReadLine();
}
static void c_TimesUp(object sender, EventArgs e) {
Console.WriteLine("Times up!");
}
}
Difference between Events and Delegates
Events are implemented using delegates, so what is the difference between an event and a delegate? The difference is that for an event you cannot directly assign a delegate to it using the =
operator; you must use the +=
operator.
To understand the difference, consider the following class definitions — Class1
and Class2
:
namespace DelegatesVsEvents {
class Program {
static void Main(string[] args) {}
}
class Class1 {
public delegate void Class1Delegate();
public Class1Delegate del;
}
class Class2 {
public delegate void Class2Delegate();
public event Class2Delegate evt;
}
}
In this code, Class1
exposes a public delegate del, of type Class1Delegate
. Class2
is similar to Class1
, except that it exposes an event evt
, of type Class2Delegate
. del
and evt
each expect a delegate, with the exception that evt
is prefixed with the event
keyword.
To use Class1
, you create an instance of Class1
and then assign a delegate to the del delegate using the " =
" operator:
static void Main(string[] args) {
//---create a delegate---
Class1.Class1Delegate d1 =
new Class1.Class1Delegate(DoSomething);
Class1 c1 = new Class1();
Читать дальше