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