Raising an Event

M

Mark

I'm trying to raise and handle an event. I'm using an example from the
VS.NET help files
(http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/ht
ml/cpconprovidingeventfunctionality.asp)

Near the bottom is the code below. Two questions:

1. Why does it check to see if Alarm doesn't equal null?
2. Whenever I use my debugger to walk through the OnAlarm method, Alarm
always equals null so the Alarm(this, e) method is never called ... why
would this be?

Thanks in advance!
Mark

public class AlarmClock
{
//Step 3. The Alarm event is defined using the event keyword.
//The type of Alarm is AlarmEventHandler.

public event AlarmEventHandler Alarm;

//Step 4. The protected OnAlarm method raises the event by invoking
//the delegates. The sender is always this, the current instance of
//the class.

protected virtual void OnAlarm(AlarmEventArgs e)
{
if (Alarm != null)
{
//Invokes the delegates.
Alarm(this, e);
}
}
}
 
N

Nicholas Paldino [.NET/C# MVP]

Mark,

The Alarm event has to be subscribed to by something in order to have
the event fire. If no one is listening, then nothing is done. This is why
it never fires. It's also why a null check needs to be done. If there are
no events, and you try and fire it, then you will get a
NullReferenceException.

Also, the pattern of checking for null on a delegate and then firing is
wrong (in respect to multithreaded operations).

To correctly fire an event, you should make your method that fires it
virtual, or indicate it should not be inlined with the MethodImpl attribute.
Then, in your method, assign the delegate to a stack based variable, and
then check that for null, and then fire it.

Hope this helps.
 
?

=?ISO-8859-2?Q?Marcin_Grz=EAbski?=

Hi Mark,

I'm trying to raise and handle an event. I'm using an example from the
VS.NET help files
(http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/ht
ml/cpconprovidingeventfunctionality.asp)

Near the bottom is the code below. Two questions:

1. Why does it check to see if Alarm doesn't equal null?

If there's no one handler added to this event then it is
equal 'null'.
2. Whenever I use my debugger to walk through the OnAlarm method, Alarm
always equals null so the Alarm(this, e) method is never called ... why
would this be?

It seems that you haven't any handler added to handle
'Alarm' event.
Thanks in advance!
Mark

public class AlarmClock
{
//Step 3. The Alarm event is defined using the event keyword.
//The type of Alarm is AlarmEventHandler.

public event AlarmEventHandler Alarm;

//Step 4. The protected OnAlarm method raises the event by invoking
//the delegates. The sender is always this, the current instance of
//the class.

protected virtual void OnAlarm(AlarmEventArgs e)
{
if (Alarm != null)
{
//Invokes the delegates.
Alarm(this, e);
}
}

try this:

// Alarm event trigger
public void PerformAlarm() {
this.OnAlarm(new AlarmEventArgs(...));
// i don't know AlarmEventArgs class
}

// in any executable code
AlarmClock alarmClock=new AlarmClock();
alarmClock.Alarm+=new AlarmEventHandler(alarmClock_Alarm);
// you'll need to define 'alarmClock_Alarm' method
alarmClock.PerformAlarm();

HTH
Marcin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top