Книга: C# 2008 Programmer

Implementing Events

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);
 }
}

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!");
 }
}

Оглавление книги


Генерация: 0.041. Запросов К БД/Cache: 0 / 0
поделиться
Вверх Вниз