/*This shortcut does not work, because the sender's type is "object" not Stack<T> as immediately above here in the longer version. * The error happens on the event handlers assigned above, they have to be the same signature, when using the shortcut the signature looks like this * EventHandler(object sender, StackEventArgs args)*/ //public EventHandler<StackEventArgs> stackEvent; public void DoWork() { int i = 0; while (i < 100) { Console.WriteLine("Doing work for line: {0}", i); i++; } StackEventArgs EventArgs = new StackEventArgs(); EventArgs.LastLineNumReached = i; EventArgs.TimeComplete = DateTime.Now; OnStackChanged(EventArgs); }
public void OnStackChanged(StackEventArgs a) { stackEvent(this, a); //This is just an example, in the real world a copy of stackEvent should be called not this original (see EventArg..Thread-Safety example file) }
//This will be the method that is called by the Stack class when the Stack class runs the method that calls the Event method that is in the Stack class //This method is called because we subscribed to the publisher above (assigned this method to the event above) //So it is the publisher calling this method passing in its instance information along with any arguments concerning the event. public void HandleStackChange <T>(Stack <T> stack, StackEventArgs args) { Console.WriteLine("I see you are done with your work, and that the last line you wrote was {0} and that you finished at {1}", args.LastLineNumReached, args.TimeComplete); }