Esempio n. 1
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** Delegates as event enablers *****\n");

            // Создать объект Car.
            Car car = new Car("Ford", 50, 100);

            // Сообщить объекту Car, какой метод вызывать,
            // когда он пожелает отправить сообщение.
            //car.RegisterWithCarEngine(new Car.CarEngineHandler(OnCarEngineEvent1));
            //car.RegisterWithCarEngine(new Car.CarEngineHandler(OnCarEngineEvent2));

            Car.CarEngineHandler handler1 = new Car.CarEngineHandler(OnCarEngineEvent1);
            car.RegisterWithCarEngine(handler1);
            Car.CarEngineHandler handler2 = new Car.CarEngineHandler(OnCarEngineEvent2);
            car.RegisterWithCarEngine(handler2);

            // Увеличить скорость (это инициирует события).
            Console.WriteLine("***** Speeding up *****");
            for (int i = 0; i < 5; i++)
            {
                car.Accelerate(10);
            }

            car.UnRegisterWithCarEngine(handler2);
            Console.WriteLine("***** Speeding up *****");
            for (int i = 0; i < 5; i++)
            {
                car.Accelerate(10);
            }

            Console.ReadLine();
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** Delegates as event enablers *****\n");
            // First, make a Car object.
            Car c1 = new Car("SlugBug", 100, 10);

            c1.RegisterWithCarEngine(new Car.CarEngineHandler(OnCarEngineEvent));
            // This time, hold onto the delegate object,
            // so we can unregister later.
            Car.CarEngineHandler handler2 = new Car.CarEngineHandler(OnCarEngineEvent2);
            c1.RegisterWithCarEngine(handler2);
            // Speed up (this will trigger the events).
            Console.WriteLine("***** Speeding up *****");
            for (int i = 0; i < 6; i++)
            {
                c1.Accelerate(20);
            }
            // Unregister from the second handler.
            c1.UnRegisterWithCarEngine(handler2);
            // We won't see the "uppercase" message anymore!
            Console.WriteLine("***** Speeding up *****");
            for (int i = 0; i < 6; i++)
            {
                c1.Accelerate(20);
            }
        }
Esempio n. 3
0
        static void RemoveTargets()
        {
            Console.ForegroundColor = ConsoleColor.DarkYellow;
            Console.WriteLine("=> Removing Targets from a Delegate's Invocation List");

            Car c1 = new Car("SlugBug", 100, 10);

            c1.RegisterWithCarEngine(new Car.CarEngineHandler(OnCarEngineEvent));

            Car.CarEngineHandler handler2 = new Car.CarEngineHandler(OnCarEngineEvent2);
            c1.RegisterWithCarEngine(handler2);
            Console.WriteLine("===== Speeding up =====");
            for (int i = 0; i < 7; i++)
            {
                c1.Accelerate(20);
            }

            c1.UnRegisterWithCarEngine(handler2);
            c1.Speed = 10;
            c1.StatusReset();
            Console.WriteLine("----- Speeding up -----");
            for (int i = 0; i < 7; i++)
            {
                c1.Accelerate(20);
            }
        }
        public Example_multicasting_delegate()
        {
            Console.WriteLine("***** Delegates as event enablers *****\n");
            // First, make a Car object.
            Car c1 = new Car("SlugBug", 100, 10);

            // Now, tell the car which method to call
            // when it wants to send us messages.
            Car.CarEngineHandler handler1 = new Car.CarEngineHandler(OnCarEngineEvent1);
            Car.CarEngineHandler handler2 = new Car.CarEngineHandler(OnCarEngineEvent2);

            c1.RegisterWithCarEngine(handler1);
            c1.RegisterWithCarEngine(handler2);
            // Remove handler1
            c1.UnRegisterWithCarEngine(handler1);
            // Speed up (this will trigger the events).
            Console.WriteLine("***** Speeding up *****");
            for (int i = 0; i < 6; i++)
            {
                c1.Accelerate(20);
            }

            Console.WriteLine("\nAfter remove handler");
            // Remove handler1
            //c1.UnRegisterWithCarEngine(handler2);

            // review
            for (int i = 0; i < 6; i++)
            {
                c1.Accelerate(20);
            }

            Console.ReadLine();
        }
Esempio n. 5
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** Delegates as event enablers ******\n");

            // First, make a Car object.
            Car c1 = new Car("Slugbug", 100, 10);

            // Now, tell the car which method to call when it wants to send us messages.
            c1.RegisterWithCarEngine(new Car.CarEngineHandler(OnCarEngineEvent));

            // This time, hold onto the delegate object, so we can unregister later.
            Car.CarEngineHandler handler2 = new Car.CarEngineHandler(OnCarEngineEvent2);
            c1.RegisterWithCarEngine(handler2);

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

            // Unregister from the second handler.
            c1.unRegisterWithCarEngine(handler2);

            c1.CurrentSpeed = 30;

            Console.WriteLine("***** Speeding up *****");
            for (int i = 0; i < 6; i++)
            {
                c1.Accelerate(20);
            }
            Console.ReadLine();
        }
Esempio n. 6
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** Delegates as event enablers *****\n");

            Car c1 = new Car("SlugBug", 100, 10);

            c1.RegisterWithCarEngine(new Car.CarEngineHandler(OnCarEngineEvent));
            Car.CarEngineHandler handler2 = new Car.CarEngineHandler(OnCarEngineEvent2);
            c1.RegisterWithCarEngine(handler2);

            // Разгоняемся (порождая события).
            Console.WriteLine("***** Speeding up *****");
            for (int i = 0; i < 6; i++)
            {
                c1.Accelerate(20);
            }

            // Отключаеи второй обработчик
            c1.UnRegisterWithCarEngine(handler2);

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

            Console.ReadLine();
        }
Esempio n. 7
0
        private static void Main(string[] args)
        {
            Console.WriteLine("***** Delegates as event enablers *****\n");

            var c1 = new Car("SlugBug", 100, 10);

            c1.RegisterWithCarEngine(new Car.CarEngineHandler(OnCarEngineEvent));
            //c1.RegisterWithCarEngine(new Car.CarEngineHandler(OnCarEngineEvent2));

            var handler2 = new Car.CarEngineHandler(OnCarEngineEvent2);

            Console.WriteLine("***** Speeding up *****");
            for (var i = 0; i < 6; i++)
            {
                c1.Accelerate(20);
            }

            c1.UnRegisterWithCarEngine(handler2);

            Console.WriteLine("***** Speeding up *****");
            for (var i = 0; i < 6; i++)
            {
                c1.Accelerate(20);
            }

            Console.ReadLine();
        }
Esempio n. 8
0
        static void Main(string[] args)
        {
            //Make a car object
            Car car1 = new Car("SlugBug", 100, 10);

            //now tell the car which method to call
            //when it wants to send a message
            car1.RegisterWithCarEngine(new Car.CarEngineHandler(OnCarEngineEvent));
            Car.CarEngineHandler handler2 = new Car.CarEngineHandler(OnCarEngineEvent2);
            car1.RegisterWithCarEngine(handler2);

            //speed up this will trigger the event
            Console.WriteLine("**** Speeding up *****");
            for (int i = 0; i < 6; i++)
            {
                car1.Accelerate(20);
            }

            //unregister from the second handler
            car1.UnRegisterWithCarEngine(handler2);

            //We wont see the uppercase message anymore
            //for (int i = 0; i < 6; i++)
            //    car1.Accelerate(20);

            Console.ReadKey();
        }
Esempio n. 9
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** Fun with Events *****\n");
            Car c1 = new Car("Slug Bug", 100, 10);

            //register event handlers
            c1.AboutToBlow += new Car.CarEngineHandler(CarIsAlmostDoomed);
            c1.AboutToBlow += new Car.CarEngineHandler(CarAboutToBlow);

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

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

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

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

            Console.ReadLine();

        }
        public static void Run()
        {
            // First, make a Car object.
            Car c1 = new Car("Jessie", 100, 10);

            // Now, tell the car which methods to call when it wants to send us messages.
            c1.RegisterWithCarEngine(new Car.CarEngineHandler(OnCarEngineEvent)); // see below for 'OnCarEngineEvent' declaration
            Car.CarEngineHandler handler2 = new Car.CarEngineHandler(OnCarEngineEvent2);
            c1.RegisterWithCarEngine(handler2);

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

            // Unregister the second handler.
            c1.UnRegisterWithCarEngine(handler2);

            // We won't see the "uppercase" message anymore!
            Console.WriteLine("***** Speeding up *****");
            for (int i = 0; i < 6; i++)
            {
                c1.Accelerate(20);
            }
        }
Esempio n. 11
0
        /// <summary>
        /// Removing targets from Delegate's Invacation List
        /// </summary>
        private void RemovingTargetsFromDelegate()
        {
            Console.WriteLine("=> Removing targets from Delegate's Invacation List: ");

            Car c1 = new Car("SlugBug", 100, 10);

            c1.RegisterWithCarEngine(new Car.CarEngineHandler(OnCarEngineEvent));

            Car.CarEngineHandler handler2 = new Car.CarEngineHandler(OnCarEngineEvent2);
            c1.RegisterWithCarEngine(handler2);

            Console.WriteLine("-> Speed Up: ");
            for (int i = 0; i < 6; i++)
            {
                c1.Accelerate(20);
            }

            c1.UnRegisterWithCarEngine(handler2);

            Console.WriteLine("-> Speed Up: ");
            for (int i = 0; i < 6; i++)
            {
                c1.Accelerate(20);
            }

            Console.WriteLine();
        }
Esempio n. 12
0
        static void Main( string[] args )
        {
            Console.WriteLine("***** Delegates as event enablers *****\n");

            // First, make a Car object.
            Car c1 = new Car("SlugBug", 100, 10);
            c1.RegisterWithCarEngine(new Car.CarEngineHandler(OnCarEngineEvent));

            // This time, hold onto the delegate object,
            // so we can unregister later.
            Car.CarEngineHandler handler2 = new Car.CarEngineHandler(OnCarEngineEvent2);
            c1.RegisterWithCarEngine(handler2);

            // Speed up (this will trigger the events).
            Console.WriteLine("***** Speeding up *****");
            for (int i = 0; i < 6; i++)
                c1.Accelerate(20);

            // Unregister from the second handler.
            c1.UnRegisterWithCarEngine(handler2);

            // We won't see the 'uppercase' message anymore!
            Console.WriteLine("***** Speeding up *****");
            for (int i = 0; i < 6; i++)
                c1.Accelerate(20);

            Console.ReadLine();
        }
Esempio n. 13
0
        private void button1_Click_1(object sender, EventArgs e)
        {
            // Створюємо об'єкт типу Car
            Car myCar = new Car("Авто Запорожець", 100, 0);

            // Створимо  метод, адресу якого будемо передавати делегату
            // Створимо змінну типу  делегат і зашлемо в неї адресу методу, який буде викликатись через цю змінну

            // Змінено

            Car.CarEngineHandler myDelegat1 = new Car.CarEngineHandler(OnCarEngineEvent1);
            Car.CarEngineHandler myDelegat2 = new Car.CarEngineHandler(OnCarEngineEvent2); // Додано
            // Звернемось до методу RegisterWithCarEngine, щоб вказати метод, який повинен викликатись (зареєструвати)
            myCar.RegisterWithCarEngine(myDelegat1);
            myCar.RegisterWithCarEngine(myDelegat2);

            // Ми можемо викликати метод OnCarEngineEvent і поза делегатом

            OnCarEngineEvent1("Стартуємо");

            // Будемо змінювати швидкість автомобіля і відслідковувати, що буде
            for (int i = 0; i < 11; i++)
            {
                myCar.Accselerate(10);
            }
        }
Esempio n. 14
0
        static void Main(string[] args)
        {
            Console.WriteLine("*******fun with events**********");
            Car car = new Car("ford", 10, 100);

            //register event handlers
            Car.CarEngineHandler handlerOk = new Car.CarEngineHandler(CarNormal);
            car.CarIsOk += handlerOk;

            Car.CarEngineHandler handlerAboutTo1 = new Car.CarEngineHandler(CarAboutToBlow);
            Car.CarEngineHandler handlerAboutTo2 = new Car.CarEngineHandler(CarIsAlmostDoomed);
            car.AboutToExplode += handlerAboutTo1 + handlerAboutTo2;

            Car.CarEngineHandler handlerDead = new Car.CarEngineHandler(CarExploded);
            car.Exploded += handlerDead;
            //end of handler registration
            Console.WriteLine("*******speeding up**********");
            for (int i = 0; i < 6; i++)
            {
                car.Accelerate(20);
            }
            //de register events
            car.Exploded -= handlerDead;
            Console.WriteLine("*************Speeding up again************");
            for (int i = 0; i < 6; i++)
            {
                car.Accelerate(20);
            }
            Console.ReadLine();
        }
Esempio n. 15
0
        static void Main(string[] args)
        {
            Console.WriteLine("**** Fun with Events ****");
            Car myCar = new Car("Mikky", 10, 100);

            myCar.AboutToBlow += new Car.CarEngineHandler(CarIsAlmostDoomed);
            myCar.AboutToBlow += new Car.CarEngineHandler(CarAboutToBlow);
            Car.CarEngineHandler exploded = new Car.CarEngineHandler(CarExploded);
            myCar.Exploded += exploded;

            //OR
            //myCar.AboutToBlow += CarIsAlmostDoomed;
            //myCar.AboutToBlow += CarAboutToBlow;
            //myCar.Exploded += CarExploded;


            Console.WriteLine("Speeding up:");
            for (int i = 0; i < 6; i++)
            {
                myCar.Accelerate(20);
            }

            myCar.Exploded -= exploded;

            //OR
            //myCar.Exploded -= CarExploded;

            Console.WriteLine("Speeding up:");
            for (int i = 0; i < 6; i++)
            {
                myCar.Accelerate(20);
            }

            Console.ReadLine();
        }
Esempio n. 16
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** Delegates as event enablers *****\n");

            // First, make a Car object.
            Car c1 = new Car("SlugBug", 100, 10);

            // Now, tell the car which method to call
            // when it wants to send us messages.
            c1.RegisterWithCarEngine(new Car.CarEngineHandler(OnCarEngineEvent));

            Car.CarEngineHandler handler2 = new Car.CarEngineHandler(OnCarEngineEvent1);
            c1.RegisterWithCarEngine(handler2);

            // Speed up (this will trigger the events).

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

            c1.UnRegisterWithCarEngine(handler2);
            for (int i = 0; i < 6; i++)
            {
                c1.Accelerate(20);
            }
            Console.ReadLine();
        }
Esempio n. 17
0
        /// <summary>
        /// Basic Event Usage
        /// </summary>
        private void BasicEventUsage()
        {
            Console.WriteLine("=> Basic Event Usage: ");

            Car c1 = new Car("SlugBug", 100, 10);

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

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

            Console.WriteLine("Speed Up");
            for (int i = 0; i < 6; i++)
            {
                c1.Accelerate(20);
            }

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

            Console.WriteLine("Speed Up");
            for (int i = 0; i < 6; i++)
            {
                c1.Accelerate(20);
            }

            Console.WriteLine();
        }
Esempio n. 18
0
        static void Main(string[] args)
        {
            Console.WriteLine("**** Delegates as even enablers ****\n");

            // First, make a car object
            Car c1 = new Car("SlugBug", 100, 10);

            // Now, tell the car which method to call when it wants to send us messages
            c1.RegisterWithCarEngine(new Car.CarEngineHandler(OnCarEngineEvent));
            // This time hold onto the delegate object, so that it can be unregistered later
            Car.CarEngineHandler handler2 = new Car.CarEngineHandler(OnCarEngineEvent2);
            c1.RegisterWithCarEngine(handler2); // unregister the delegate object

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

            c1.UnregisterWithCarEngine(handler2);
            c1.CurrentSpeed = 0;

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

            Console.ReadLine();
        }
Esempio n. 19
0
        static void DelegateAsEventEnables_remove()
        {
            System.Console.WriteLine("=> delegate as event enablers: remove");

            Car c1 = new Car("ZL", 100, 10);

            c1.RegisterWithCarEngine(new Car.CarEngineHandler(OnCarEngineEvent));

            Car.CarEngineHandler hanlder2 = new Car.CarEngineHandler(OnCarEngineEvent2);
            c1.RegisterWithCarEngine(hanlder2);

            // speed up
            System.Console.WriteLine("***** Speed Up*****");
            for (int i = 0; i < 6; ++i)
            {
                c1.Accelerate(20);
            }
            Console.ReadLine();

            // unregister from the second handler
            c1.UnRegisterWithCarEngine(hanlder2);
            // speed up
            System.Console.WriteLine("***** Speed Up*****");
            for (int i = 0; i < 6; ++i)
            {
                c1.Accelerate(20);
            }

            Console.ReadLine();
        }
Esempio n. 20
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** Delegates as event enablers *****\n");

            // First, make a Car object.
            Car c1 = new Car("SlugBug", 100, 10);

            // Now, tell the car which method to call
            // when it wats to send us messages.
            c1.RegisterWithCarEngine(new Car.CarEngineHandler(OnCarEngineEvent));

            // This time, hold onto the delegate object,
            // so we can unregister later
            Car.CarEngineHandler handler2 = new Car.CarEngineHandler(OnCarEngineEvent2);
            c1.RegisterWithCarEngine(handler2);

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

            // Unregister from the second handler.
            c1.UnregisterWithCarEngine(handler2);

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

            Console.ReadKey();
        }
Esempio n. 21
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** Delegates as event enablers *****\n");

            //  首先,创建一个Car对象
            Car c1 = new Car("SlugBug", 100, 10);

            //  现在,告诉汽车,他想要向我们发送信息时调用哪个方法
            c1.RegisterWithCarEngine(new Car.CarEngineHandler(OnCarEngineEvent));

            //  先绑定委托对象,稍后再注销
            Car.CarEngineHandler handler2 = new Car.CarEngineHandler(OnCarEngineEvent2);
            c1.RegisterWithCarEngine(handler2);

            //  加速 (这将触发事件)
            Console.WriteLine("***** Speeding Up *****");
            for (int i = 0; i < 6; i++)
            {
                c1.Accelerate(20);
            }

            //  注销第二个处理程序
            c1.UnRegisterWithCarEngine(handler2);

            //  看不到大写的消息了
            Console.WriteLine("***** Speeding up *****");
            for (int i = 0; i < 6; i++)
            {
                c1.Accelerate(20);
            }

            Console.ReadLine();
        }
Esempio n. 22
0
        static void EventKeyword()
        {
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("=> Events");

            Car c1 = new Car("SlugBug", 100, 10);

            //c1.AboutToBlow += CarIsAlmostDoomed; //method group conversion
            //c1.AboutToBlow += CarAboutToBlow;
            c1.AboutToBlow += new Car.CarEngineHandler(CarIsAlmostDoomed);
            c1.AboutToBlow += new Car.CarEngineHandler(CarAboutToBlow);

            Car.CarEngineHandler d = new Car.CarEngineHandler(CarExploded);
            c1.Exploaded += d;

            Console.WriteLine("===== Speeding up");
            for (int i = 0; i < 7; i++)
            {
                c1.Accelerate(20);
            }

            c1.Exploaded -= d;

            c1.Reset();
            Console.WriteLine("----- Speeding up");
            for (int i = 0; i < 7; i++)
            {
                c1.Accelerate(20);
            }
        }
Esempio n. 23
0
        static void Main(string[] args)
        {
            Console.WriteLine("Delegates as event enablers");

            Car c1 = new Car("NCC-1701", 10, 6);

            //register an extra handler
            c1.RegisterWithCarEngine(new Car.CarEngineHandler(OnCarEngineEvent));
            //register a handler for events generated in the car's engine
            //store the delegate type instance as a variable for later de-registration
            Car.CarEngineHandler handler2 = new Car.CarEngineHandler(OnCarEngineEvent2);
            c1.RegisterWithCarEngine(handler2);

            //engage
            Console.WriteLine("Engaging warp 11");
            for (int i = 0; i < 6; i++)
            {
                c1.Accelerate(1);
                if (c1.CurrentSpeed >= c1.MaxSpeed)
                {
                    //Scotty gives up in despair
                    c1.UnrgeisterWithCarEngine(handler2);
                }
            }

            Console.ReadLine();
        }
Esempio n. 24
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** Delegates as event enablers *****\n");

            // First, make a Car object.
            Car c1 = new Car("SlugBug", 100, 10);

            // Now, tell the car which method to call
            // when it wants to send us messages.
            c1.RegisterWithCarEngine(new Car.CarEngineHandler(OnCarEngineEvent));

            // Register multiple targets for the notifications.
            Car.CarEngineHandler handler2 = new Car.CarEngineHandler(OnCarEngineEvent2);
            c1.RegisterWithCarEngine(handler2);


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

            // Unregister from the second handler.
            c1.UnRegisterWithCarEngine(handler2);

            // We won't see the "uppercase" message anymore!
            Console.WriteLine("***** Speeding up Again*****");
            for (int i = 0; i < 6; i++)
            {
                c1.Accelerate(20);
            }

            Console.WriteLine("***** Method Group Conversion *****\n");
            Car c2 = new Car();

            // Register the simple method name.
            c2.RegisterWithCarEngine(OnCarEngineEvent);

            Console.WriteLine("***** Speeding up *****");
            for (int i = 0; i < 6; i++)
            {
                c2.Accelerate(20);
            }

            // Unregister the simple method name.
            c2.UnRegisterWithCarEngine(OnCarEngineEvent);

            // No more notifications!
            for (int i = 0; i < 6; i++)
            {
                c2.Accelerate(20);
            }


            Console.ReadLine();
        }
Esempio n. 25
0
        static void Main(string[] args)
        {
            Car carAlfaRomeo = new Car("AlfaRomeo", 100, 10);

            carAlfaRomeo.RegisterWithCarEngine(new Car.CarEngineHandler(OnCarEngineEvent));
            Car.CarEngineHandler carHandler = new Car.CarEngineHandler(OnCarEngineEventNew);
            carAlfaRomeo.RegisterWithCarEngine(carHandler);
            Console.WriteLine("Speeding up!");
            for (int i = 0; i < 6; i++)
            {
                carAlfaRomeo.Accelerate(20);
            }
            carAlfaRomeo.UnregisterWithCarEngine(carHandler);
        }
        static void Main(string[] args)
        {
            Console.WriteLine("*********Delegate as event enablers*******\n");
            Car c1 = new Car("SlugBug", 100, 10);

            Car.CarEngineHandler xxxd = new Car.CarEngineHandler(OnCarEngineEvent);//instantiate a delegate
            //c1.RegisterWithCarEngine(new Car.CarEngineHandler(OnCarEngineEvent));
            c1.RegisterWithCarEngine(xxxd);
            Console.WriteLine("***** Speeding up *****");
            for (int i = 0; i < 6; i++)
            {
                c1.Accelerate(20);
            }
            Console.ReadLine();
        }
Esempio n. 27
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** Delegates as event enablers *****\n");

            // First, make a Car object.
            Car c1 = new Car("SlugBug", 100, 10);

            c1.RegisterWithCarEngine(new Car.CarEngineHandler(OnCarEngineEvent));

            // This time, hold onto the delegate object,
            // so we can unregister later.
            Car.CarEngineHandler handler2 = new Car.CarEngineHandler(OnCarEngineEvent2);
            c1.RegisterWithCarEngine(handler2);
            // Speed up (this will trigger the events).
            Console.WriteLine("***** Speeding up *****");
            for (int i = 0; i < 6; i++)
            {
                c1.Accelerate(20);
            }

            // Unregister from the second handler.
            c1.UnRegisterWithCarEngine(handler2);

            // We won't see the "uppercase" message anymore!
            Console.WriteLine("***** Speeding up *****");
            for (int i = 0; i < 6; i++)
            {
                c1.Accelerate(20);
            }

            Console.WriteLine("***** Agh! No Encapsulation! *****\n");
            // Make a Car.
            Car myCar = new Car();

            // We have direct access to the delegate!
            myCar.listOfHandlers = new Car.CarEngineHandler(CallWhenExploded);
            myCar.Accelerate(10);

            // We can now assign to a whole new object...
            // confusing at best.
            myCar.listOfHandlers = new Car.CarEngineHandler(CallHereToo);
            myCar.Accelerate(10);

            // The caller can also directly invoke the delegate!
            myCar.listOfHandlers.Invoke("hee, hee, hee...");

            Console.ReadLine();
        }
Esempio n. 28
0
        static void Main(string[] args)
        {
            Car c1 = new Car("甲壳虫", 300, 200);

            c1.RegisterWithCarEngine(new Car.CarEngineHandler(OnCarEngineEvent));

            Car.CarEngineHandler handler2 = new Car.CarEngineHandler(OnCarEngineEvent);
            c1.RegisterWithCarEngine(handler2);

            for (int i = 0; i < 100; i++)
            {
                c1.Accelerate(10);
            }
            //c1.DisplayCurrSp();

            Console.ReadKey();
        }
        static void Main(string[] args)
        {
            Console.WriteLine("***** Delegates as event enables *****\n");

            Car c1 = new Car("SlugBug", 100, 10);

            c1.RegisterWithCarEngine(new Car.CarEngineHandler(OnCarEngineEvent));

            Car.CarEngineHandler engineHandler = new Car.CarEngineHandler(OnCarEngineEvent2);
            c1.RegisterWithCarEngine(new Car.CarEngineHandler(OnCarEngineEvent2));

            c1.UnRegisterWithCarEngine(engineHandler);
            Console.WriteLine("******Speeding up!********");
            for (int i = 0; i < 6; i++)
            {
                c1.Accelerate(20);
            }
        }
Esempio n. 30
0
        static void Main(string[] args)
        {
            Console.WriteLine("**** Delegated as event enablers ****");
            Car c1 = new Car("Jimmy", 0, 110);

            c1.RegisterWithCarEngine(new Car.CarEngineHandler(OnCarEngineEvent));
            Car.CarEngineHandler handler = new Car.CarEngineHandler(OnCarEngineEvent2);
            c1.RegisterWithCarEngine(handler);
            for (int i = 0; i < 7; i++)
            {
                c1.Accelerate(20);
            }

            c1.UnRegisterWithCarEngine(handler);
            c1.Accelerate(10);

            Console.ReadLine();
        }
Esempio n. 31
0
        //  1. define a new delegate type that will be used to send notifications to the caller
        //  2. declare a member variable of this delegate in teh Car class
        //  3. create a helper function on the Car that allows the caller to specify the method to call back on
        //  4. implement the Accelerate() method to invoke the delegate's invocation list under the correct circumstances

        static void Main(string[] args)
        {
            Console.WriteLine("***** Delegates as Event Enablers *****");

            //  first make a Car object
            Car freddy = new Car("Freddy", 100, 20);

            //  next tell the car which method to call when it wants to send us messages
            freddy.RegisterWithCarEngine(new Car.CarEngineHandler(OnCarEngineEvent));

            //  register a secont target for the notification
            //      ...but hold onto it so unsubscribe can be done later
            Car.CarEngineHandler handler2 = new Car.CarEngineHandler(OnCarEngineEvent2);

            //  time has passed so register now
            freddy.RegisterWithCarEngine(handler2);

            //  speed up to trigger the events
            Console.WriteLine("***** Speeding up... *****");
            for (int i = 0; i < 9; i++)
            {
                freddy.Accelerate(15);
            }

            //  unregister from the second handler
            freddy.UnRegisterWithCarEngine(handler2);

            //  slow the car down
            freddy.CurrentSpeed = 30;
            if (freddy.FixMyCar())
            {
                Console.WriteLine($"{ freddy.PetName } is fixed! Current speed is: { freddy.CurrentSpeed }.\n");
            }

            //  no more UPPERCASE messages after unregistering
            Console.WriteLine("***** Speeding up... *****");
            for (int i = 0; i < 9; i++)
            {
                freddy.Accelerate(15);
            }

            Console.WriteLine("Press <Enter> to exit. . .");
            Console.ReadLine();
        }
Esempio n. 32
0
        static void NullConditional()
        {
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("=> Events with Null-Conditional Operator");

            Car c1 = new Car("SlugBug", 100, 10);

            c1.AboutToBlow += CarIsAlmostDoomed;
            c1.AboutToBlow += CarAboutToBlow;

            Car.CarEngineHandler d = new Car.CarEngineHandler(CarExploded);
            c1.Exploaded += d;

            Console.WriteLine("===== Speeding up");
            for (int i = 0; i < 7; i++)
            {
                c1.Accelerate2(20);
            }
        }
Esempio n. 33
0
        static void Main(string[] args) {
            Car c1 = new Car("SlugBug", 100, 10);
            //c1.RegisterWithCarEngine(OnCarEngineEvent);          Also works fine, method group conversion
            c1.RegisterWithCarEngine(new Car.CarEngineHandler(OnCarEngineEvent));

            Car.CarEngineHandler handler2 = new Car.CarEngineHandler(OnCarEngineEvent2);
            c1.RegisterWithCarEngine(handler2);

            for (int i = 0; i < 6; i++) {
                c1.Accelerate(20);
            }

            c1.UnRegisterWithCarEngine(handler2);
            for (int i = 0; i < 2; i++) {
                c1.Accelerate(20);
            }

            Console.ReadLine();
        }
        public static void Main(string[] args)
        {
            Car.CarEngineHandler carAboutToBlow = new Car.CarEngineHandler(CarAboutToBlow);
            Car.CarEngineHandler exploded       = new Car.CarEngineHandler(CarIsDoomed);

            Car c = new Car("Zippy", 100, 20);

            c.Exploded    += exploded;
            c.AboutToBlow += carAboutToBlow;

            // OR USING METHOD GROUP COVERSION ASSISTANCE PROVIDED BY VISUAL STUDIO

            c.AboutToBlow += c_AboutToBlow;

            for (int i = 0; i < 20; i++)
            {
                c.Accelerate(5);
            }
            Console.ReadKey();
        }
Esempio n. 35
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** Delegates as event enablers *****\n");
            //make a car
            Car c1 = new Car("SlugBug", 100, 10);
            //now tell the car which method(s) to call
            c1.RegisterWithCarEngine(new Car.CarEngineHandler(OnCarEngineEvent));

            //we want an object that we can subsequently UNREGISTER...
            Car.CarEngineHandler handler2 = new Car.CarEngineHandler(OnCarEngineEvent2);
            c1.RegisterWithCarEngine(handler2);

            //UNREGISTER the second handler
            c1.UnRegisterWithCarEngine(handler2);//enabling this line will UNREGISTER the second
            //event

            //Speed up (this will trigger events)
            Console.WriteLine("***** Speeding up *****");
            for (int i = 0; i < 6; i++)
                c1.Accelerate(20);

            Console.ReadLine();
        }
        public Example_multicasting_delegate()
        {
            Console.WriteLine("***** Delegates as event enablers *****\n");
            // First, make a Car object.
            Car c1 = new Car("SlugBug", 100, 10);
            // Now, tell the car which method to call
            // when it wants to send us messages.   
            Car.CarEngineHandler handler1 = new Car.CarEngineHandler(OnCarEngineEvent1);
            Car.CarEngineHandler handler2 = new Car.CarEngineHandler(OnCarEngineEvent2);

            c1.RegisterWithCarEngine(handler1);
            c1.RegisterWithCarEngine(handler2);
            // Remove handler1
            c1.UnRegisterWithCarEngine(handler1);
            // Speed up (this will trigger the events).
            Console.WriteLine("***** Speeding up *****");
            for (int i = 0; i < 6; i++)
                c1.Accelerate(20);

            Console.WriteLine("\nAfter remove handler");
            // Remove handler1
            //c1.UnRegisterWithCarEngine(handler2);

            // review
            for (int i = 0; i < 6; i++)
                c1.Accelerate(20);

            Console.ReadLine();
        }