PC Review


Reply
Thread Tools Rate Thread

Delegates and NullReferenceException

 
 
Paul
Guest
Posts: n/a
 
      9th Jan 2007
Hello All,

In the following code I was expecting to get a NullReferenceException,
but that didn't happen. I was hoping someone could explain why.

I have two classes, Form1 and Ticker. As soon as Form1 is loaded I am
expecting the Timer's elapsed event, ticking_Elapsed, to happen once
every second and for a NullReferenceException to occur since ticking is
not initialized, but nothing happens. I added the
MessageBox.Show("Done") to check if the ticking_Elapsed event really
was happening. The interesting part is that the message box does not
appear unless I comment out the code that calls the private Notify
method.

Why is there no NullReferenceException?

Any help is appreciated. Thank you.

Paul

public partial class Form1 : Form
{
private Ticker pulsed = new Ticker();

public Form1()
{
InitializeComponent();
}
}

class Ticker
{
public delegate void Tick(int hh, int mm, int ss);

private Tick tickers;
private Timer ticking = new Timer();

public Ticker()
{
this.ticking.Elapsed += new
ElapsedEventHandler(ticking_Elapsed);
this.ticking.Interval = 1000;
this.ticking.Enabled = true;
}

private void ticking_Elapsed(object sender, ElapsedEventArgs e)
{
Notify(1, 2, 3);
System.Windows.Forms.MessageBox.Show("Done");
}

public void Add(Tick newMethod)
{
this.tickers += newMethod;
}

public void Remove(Tick oldMethod)
{
this.tickers -= oldMethod;
}

private void Notify(int hours, int minutes, int seconds)
{
this.tickers(hours, minutes, seconds);
}
}

 
Reply With Quote
 
 
 
 
Stoitcho Goutsev \(100\)
Guest
Posts: n/a
 
      9th Jan 2007
Paul,

It depends how you use the timer object (Ticker in your case) If the tick
even is rasied in a saparate thread, which is the default behavior you
probably won't get the exception as exceptions does not cross thread
boundaries. What you can do is to use synchornizing object (look at
Timer.SynchornizingObject property) in order to marshal back the elapsed
event to the main UI thread. Then you will probably get the exception.


--
Stoitcho Goutsev (100)

"Paul" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hello All,
>
> In the following code I was expecting to get a NullReferenceException,
> but that didn't happen. I was hoping someone could explain why.
>
> I have two classes, Form1 and Ticker. As soon as Form1 is loaded I am
> expecting the Timer's elapsed event, ticking_Elapsed, to happen once
> every second and for a NullReferenceException to occur since ticking is
> not initialized, but nothing happens. I added the
> MessageBox.Show("Done") to check if the ticking_Elapsed event really
> was happening. The interesting part is that the message box does not
> appear unless I comment out the code that calls the private Notify
> method.
>
> Why is there no NullReferenceException?
>
> Any help is appreciated. Thank you.
>
> Paul
>
> public partial class Form1 : Form
> {
> private Ticker pulsed = new Ticker();
>
> public Form1()
> {
> InitializeComponent();
> }
> }
>
> class Ticker
> {
> public delegate void Tick(int hh, int mm, int ss);
>
> private Tick tickers;
> private Timer ticking = new Timer();
>
> public Ticker()
> {
> this.ticking.Elapsed += new
> ElapsedEventHandler(ticking_Elapsed);
> this.ticking.Interval = 1000;
> this.ticking.Enabled = true;
> }
>
> private void ticking_Elapsed(object sender, ElapsedEventArgs e)
> {
> Notify(1, 2, 3);
> System.Windows.Forms.MessageBox.Show("Done");
> }
>
> public void Add(Tick newMethod)
> {
> this.tickers += newMethod;
> }
>
> public void Remove(Tick oldMethod)
> {
> this.tickers -= oldMethod;
> }
>
> private void Notify(int hours, int minutes, int seconds)
> {
> this.tickers(hours, minutes, seconds);
> }
> }
>



 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can we declare delegates in class and can we declare delegates in Interface Bhuwan Bhaskar Microsoft Dot NET Framework 4 4th Oct 2007 05:25 AM
NullReferenceException handler throws NullReferenceException! Zytan Microsoft C# .NET 7 19th Mar 2007 09:27 PM
NullReferenceException - but where?! =?Utf-8?B?bXVzb3NkZXY=?= Microsoft ASP .NET 1 28th Nov 2006 10:20 AM
Advanced use of delegates to automatically disconnect delegates from a set of events kristian.freed@gmail.com Microsoft C# .NET 2 18th Nov 2006 09:56 PM
PinvokeDLL or delegates or ??? ... Calling C# delegates from C++ as CALLBACK Roland Rehmnert Microsoft Dot NET Compact Framework 1 20th Feb 2005 12:21 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:08 AM.