private static void TestEvents() { //Create an object of class tape recorder TapeRecorder objTapeRecorder = new TapeRecorder(); PlayTapeRecorder objPlayTapeRecorder = new PlayTapeRecorder(); objTapeRecorder.Play += objPlayTapeRecorder.PlayTape; objTapeRecorder.PlayReturns += objPlayTapeRecorder.PlayTapeWithReturns; objTapeRecorder.PlayReturns += objPlayTapeRecorder.PlayTapeWithReturns2; objTapeRecorder.Pause += objPlayTapeRecorder.PlayTape; objTapeRecorder.FastForward += objPlayTapeRecorder.PlayTape; objTapeRecorder.Reverse += objPlayTapeRecorder.PlayTape; //Invoke the events objTapeRecorder.RaiseTheEvents(); Console.ReadLine(); }
private static void TestEventsWithThreading() { Console.WriteLine("Registering events on the managed thread: " + System.Threading.Thread.CurrentThread.ManagedThreadId); //The idea here is to register the events on one thread and raise the events on other thread and check which thread executes the handler //FINDING: The thread on which the event is raised is the one that actually calls the event handler as well. TapeRecorder objTapeRecorder = new TapeRecorder(); PlayTapeRecorder objPlayTapeRecorder = new PlayTapeRecorder(); objTapeRecorder.Pause += objPlayTapeRecorder.PlayPause; objTapeRecorder.Play += objPlayTapeRecorder.PlayTape; //objTapeRecorder.PlayReturns += objPlayTapeRecorder.PlayTapeWithReturns; //objTapeRecorder.PlayReturns += objPlayTapeRecorder.PlayTapeWithReturns2; //objTapeRecorder.Pause += objPlayTapeRecorder.PlayTape; //objTapeRecorder.FastForward += objPlayTapeRecorder.PlayTape; //objTapeRecorder.Reverse += objPlayTapeRecorder.PlayTape; //Invoke the events objTapeRecorder.RaiseTheEventsInOtherThread(); }