473,327 Members | 2,081 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Raising an Event

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/de...-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);
}
}
}


Nov 16 '05 #1
2 2176
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.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Mark" <fi******@idonotlikejunkmail.umn.edu> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
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/de...-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);
}
}
}


Nov 16 '05 #2
Hi Mark,

<snip>
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/de...-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
Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Rajesh.V | last post by:
I have made a custom control in asp.net using webcontrols, and am raising my event. I find that if no control is hooked into the event it goes for a toss. I tried to find length of the delegate...
6
by: Dan | last post by:
I've created a pocketpc app which has a startup form containing a listview. The form creates an object which in turn creates a System.Threading.Timer. It keeps track of the Timer state using a...
4
by: Dave A | last post by:
I am developing a somewhat complex component at the moment and coincidently I am also reading the Framework Design Guidelines book. After reading the section about event raising I have re-written...
2
by: Gman | last post by:
Hi, I have created a usercontrol, a grid control essentially. Within it I have a class: clsGridRecord. I have coded the events such that when a user clicks on the grid, say, the events occur on...
4
by: sloan | last post by:
I"m trying to figure out what concept I'm missing here, or if its not a good idea .. or what. Here is my example.. code is below. I have an employee class. It has an event that can be raised....
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.