I'm currently working on a project and I am wondering if someone can help because I am not sure how NT works with respect to this.
Basically, I have a form that is created asynchronously in my strategy (so the form pops up and if I close it, NT doesn't close, and NT can also keep on working in the background). Now, I want to display data from the strategy on the form. I am using an event listener that should be listening to an event in the strategy and then updating display info on the form. I have some sample code that works outside of NT (just a simple version I made to understand how everything works). This sample works and provides the data I need.
However, when I try to do it in NT, I get an error that states "Object reference not set to an instance of an object". This happens when I try to register my listener. I have a property in my form that sets the "base strategy" (the one that opened the form) to the instance I want to listen on (using a "this" reference when the form is created).
I have attatched an example to clarify this. It is modeled after an example I found online, but adapted to what I am trying to do. It's just a metronome that ticks, and a listener that listens, and prints a "Heard it at x" when the metronome ticks. Think of it as my form listening for new data on the strategy and displaying it when it happens.
Here is the code:
using System; namespace Project2 { // Event arg class public class TimeOfTick : EventArgs { private DateTime TimeNow; public DateTime Time { set { TimeNow = value; } get { return this.TimeNow; } } } // Metronome class is like the "strategy" public class Metronome { // This is for the event listening wihthin the form (in this case the form is the listener) public event TickHandler Tick; public delegate void TickHandler(Metronome m, TimeOfTick e); // This is for the asynchronous call to the listener (the form) public delegate void ListenerDelegate(); public static void myCallback(IAsyncResult result) { ListenerDelegate del = (ListenerDelegate)result.AsyncState; del.EndInvoke(result); } // This is a method for starting the listener asynchronously public void MetronomeStarter() { Listener listen = new Listener(); listen.MetronomeItem = this; // This is where I set the property to this. In my case, the metronome is my strategy and when I launch the form, I tell the form that this is the straetgy class I am listening on... ListenerDelegate del = new ListenerDelegate(listen.Starter); del.BeginInvoke(myCallback, del); } // This would be the section of the strategy that generates data - this is what I am listening to public void Start() { while (true) { System.Threading.Thread.Sleep(3000); if (Tick != null) // This is where the event is triggered if someone is listening { TimeOfTick TOT = new TimeOfTick(); TOT.Time = DateTime.Now; Tick(this, TOT); } } } } // This is the listener, which is the form in my case. public class Listener { private Metronome metronomeItem; public Metronome MetronomeItem // This public property sets my private metronome variable { get { return metronomeItem; } set { metronomeItem = value; } } public void Starter() // This initializes the class (in my case the form) { Console.WriteLine("Starting Listener class"); metronomeItem.Tick += new Metronome.TickHandler(HeardIt); // ... and this is what creates the error - "object reference not set to an instance of an object" } void HeardIt(Metronome m, TimeOfTick e) { System.Console.WriteLine("HEARD IT AT {0}", e.Time); // This is the result of the l } } class Test { static void Main() // This is what I am not sure about. How does NT do this. I think this is why the error occurs. { Metronome m = new Metronome(); m.MetronomeStarter(); m.Start(); } } }
Cheers,
pmorissette
Comment