示例#1
0
        public static void CarDelegateExample()
        {
            var car = new CarDelegate(10, 5, 1000)
            {
                Tank = 50,
                LastCheckEngineMileAge = 20
            };

            car.LowFuelHandler += (double fuel) => {
                Console.WriteLine($"Low fuel!!! {fuel}");
            };

            car.CheckEngineHandler += (double mileAge) => {
                Console.WriteLine($"Check Engine!!! {mileAge}");
            };

            car.Move(100);

            car.CheckEngineHandler(25);
        }
示例#2
0
        public static void CarDelegateExample()
        {
            var car = new CarDelegate(10, 5, 1000)
            {
                Tank = 50,
                LastCheckEngineMileAge = 20
            };

            car.LowFuelEvent += OnLowFuelEvent;

            car.CheckEngineEvent += (double mileAge) => {
                Console.WriteLine($"Check Engine!!! {mileAge}");
            };

            car.CheckEngineEvent += (double mileAge) => {
                Console.WriteLine("blablabla");
            };

            car.Move(100);

            //car.CheckEngineEvent(25);
        }
        static void Main(string[] args)
        {
            Stopwatch stopWatch = new Stopwatch();

            stopWatch.Start();
            string        CurrentDirection = null;
            List <Car>    Cars             = new List <Car>();
            List <Thread> Threads          = new List <Thread>();
            Program       p            = new Program();
            Random        rnd          = new Random();
            CarDelegate   cd           = new CarDelegate(p.CarRollCall);
            int           numberOfCars = rnd.Next(1, 16);

            for (int i = 1; i <= numberOfCars; i++)
            {
                if (rnd.Next(1, 3) == 1)
                {
                    Cars.Add(new Car(i, "North"));
                }
                else
                {
                    Cars.Add(new Car(i, "South"));
                }
            }
            cd(Cars);
            Console.WriteLine("\t\t\t*\t*\t*");
            while (Cars.Count > 0)
            {
                for (int i = 0; i < Cars.Count; i++)
                {
                    if (Cars[i].T.ThreadState.ToString() == "Running")
                    {
                        continue;
                    }
                    else if (Cars[i].T.ThreadState.ToString() == "Stopped")
                    {
                        Cars.Remove(Cars[i]);
                    }
                    else if (Cars[i].T.ThreadState.ToString() == "Unstarted")
                    {
                        if (i == 0)
                        {
                            Cars[i].T.Start(Cars[i]);
                            CurrentDirection        = Cars[i].Direction;
                            Console.ForegroundColor = ConsoleColor.Green;
                            Console.WriteLine("Car " + Cars[i].OrderNumber + " is starting passage over the bridge in direction " + Cars[i].Direction + ".");
                            Console.ForegroundColor = ConsoleColor.Gray;
                        }
                        else if (CurrentDirection == Cars[i].Direction && Cars[i].T.ThreadState.ToString() == "Unstarted")
                        {
                            Cars[i].T.Start(Cars[i]);
                            Console.ForegroundColor = ConsoleColor.Green;
                            Console.WriteLine("Car " + Cars[i].OrderNumber + " is starting passage over the bridge in direction " + Cars[i].Direction + ".");
                            Console.ForegroundColor = ConsoleColor.Gray;
                        }
                        else
                        {
                            Console.ForegroundColor = ConsoleColor.Yellow;
                            Console.WriteLine("Car " + Cars[i].OrderNumber + " is waiting for safe passage over the bridge in direction " + Cars[i].Direction + ".");
                            Console.ForegroundColor = ConsoleColor.Gray;
                        }
                    }
                }
                Thread.Sleep(500);
            }
            stopWatch.Stop();
            string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", stopWatch.Elapsed.Hours, stopWatch.Elapsed.Minutes, stopWatch.Elapsed.Seconds, stopWatch.Elapsed.Milliseconds / 10);

            Console.WriteLine("Application run time " + elapsedTime);
            Console.ReadLine();
        }