static void Main(string[] args)
        {
            Console.WriteLine("***** Events *****");

            // Make a car as usual.
            Car c1 = new Car("SlugBug", 100, 10);

            // Register event handlers.
            c1.AboutToBlow += new Car.CarEventHandler(CarIsAlmostDoomed);
            c1.AboutToBlow += new Car.CarEventHandler(CarAboutToBlow);

            Car.CarEventHandler d = new Car.CarEventHandler(CarExploded);
            c1.Exploded += d;

            // Speed up (this will generate the events.)
            Console.WriteLine("\n***** Speeding up *****");
            for (int i = 0; i < 6; i++)
            {
                c1.SpeedUp(20);
            }

            // Remove CarExploded method
            // from invocation list.
            c1.Exploded -= d;

            Console.WriteLine("\n***** Speeding up *****");
            for (int i = 0; i < 6; i++)
            {
                c1.SpeedUp(20);
            }

            Console.ReadLine();
        }
Esempio n. 2
0
        public static int Main(string[] args)
        {
            // Make a car as usual.
            Car c1 = new Car("SlugBug", 100, 10);

            // Make sink object.
            CarEventSink sink = new CarEventSink();

            // Hook into events.
            Car.Exploded    += new Car.EngineHandler(sink.OnBlowUp);
            Car.Exploded    += new Car.EngineHandler(sink.OnBlowUp2);
            Car.AboutToBlow += new Car.EngineHandler(sink.OnAboutToBlow);

            // Speed up (this will generate the events.)
            for (int i = 0; i < 10; i++)
            {
                c1.SpeedUp(20);
            }

            // Detach from events.
            Car.Exploded -= new Car.EngineHandler(sink.OnBlowUp);
            Car.Exploded -= new Car.EngineHandler(sink.OnBlowUp2);
            Car.Exploded -= new Car.EngineHandler(sink.OnAboutToBlow);

            // No response!
            for (int i = 0; i < 10; i++)
            {
                c1.SpeedUp(20);
            }

            return(0);
        }