Книга: C# 2008 Programmer

Passing State Information to an Event Handler

Passing State Information to an Event Handler

In the preceding program, you simply raise an event in the AlarmClock class; there is no passing of information from the class back to the event handler. To pass information from an event back to an event handler, you need to implement your own class that derives from the EventArgs base class.

In this section, you modify the previous program so that when the set time is up, the event passes a message back to the event handler. The message is set when you instantiate the AlarmClock class.

First, define the AlarmClockEventArgs class that will allow the event to pass back a string to the event handler. This class must derive from the EventArgs base class:

public class AlarmClockEventArgs : EventArgs {
 public AlarmClockEventArgs(string Message) {
  this.Message = Message;
 }
 public string Message { get; set; }
}

Next, define a delegate called AlarmClockEventHandler with the following signature:

public delegate void AlarmClockEventHandler(object sender, AlarmClockEventArgs e);

Replace the original TimesUp event statement with the following statement, which uses the AlarmClockEventHandler class:

//---public event EventHandler TimesUp;---
public event AlarmClockEventHandler TimesUp;

Add a Message property to the class so that users of this class can set a message that will be returned by the event when the time is up:

public string Message { get; set; }

Modify the onTimesUp virtual method by changing its parameter type to the new AlarmClockEventArgs class:

protected virtual void onTimesUp(AlarmClockEventArgs e) {
 if (TimesUp != null) TimesUp(this, e);
}

Finally, modify the t_Elapsed event handler so that when you now call the onTimesUp() method, you pass in an instance of the AlarmClockEventArgs class containing the message you want to pass back to the event handler:

void t_Elapsed(object sender, ElapsedEventArgs e) {
 if (DateTime.Now >= this.AlarmTime) {
  onTimesUp(new AlarmClockEventArgs(this.Message));
  t.Stop();
 }
}

Here's the complete program: using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
public class AlarmClockEventArgs : EventArgs {
 public AlarmClockEventArgs(string Message) {
  this.Message = Message;
 }
 public string Message { get; set; }
}
public delegate void AlarmClockEventHandler(object sender, AlarmClockEventArgs e);
class AlarmClock {
 Timer t;
 public event AlarmClockEventHandler TimesUp;
 protected virtual void onTimesUp(AlarmClockEventArgs e) {
  if (TimesUp != null) TimesUp(this, e);
 }
 public DateTime AlarmTime { get; set; }
 public string Message { get; set; }
 public AlarmClock() {
  t = new Timer(100);
  t.Elapsed += new ElapsedEventHandler(t_Elapsed);
 }
 public void Start() {
  t.Start();
 }
 void t_Elapsed(object sender, ElapsedEventArgs e) {
  if (DateTime.Now >= this.AlarmTime) {
   onTimesUp(new AlarmClockEventArgs(this.Message));
   t.Stop();
  }
 }
}

With the modified AlarmClock class, your program will now look like this:

namespace Events {
 class Program {
  static void c_TimesUp(object sender, AlarmClockEventArgs e) {
   Console.WriteLine(DateTime.Now.ToShortTimeString() + ": " + e.Message);
  }
  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),
    Message = "Meeting with customer."
   };
   c.TimesUp += new AlarmClockEventHandler(c_TimesUp);
   c.Start();
   Console.ReadLine();
  }
 }
}

Figure 7-10 shows the output when the AlarmClock fires the TimesUp event.


Figure 7-10

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


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