Beispiel #1
0
        /// <summary>
        /// This method starts all cars driving. It waits for all
        /// car threads to start but will not allow the cars to move
        /// until they are all ready. All the cars are told to start
        /// driving at the same time.
        /// </summary>
        /// <param name="UseThreadPool">If true, then a ThreadPool worker thread is used instead of creating a new one for each car</param>
        public static void DriveAllCars(Car.ThreadingModel Model = Car.ThreadingModel.ManualThreads)
        {
            // Use a shallow copy of all cars because AllCars
            // might be modified before all cars start to drive
            foreach (Car nextCar in CopyOfAllCars)
            {
                // Starts the thread that moves car
                nextCar.Drive(Model);

                // Wait a bit before starting the next car
                Random rnd       = new Random();
                int    waitSleep = rnd.Next(100, 201); // Creates a number between 100 and 200

                Thread.Sleep(waitSleep);
            }

            // Wait for all cars to be ready to go
            WaitForAllCars();

            // Reset the CarIsReadyToGo event on all cars to be non-signaled.
            // This will allow the code calling this "DriveAllCars" method to
            // use the "WaitForAllCars" method to wait for all cars to complete
            // their trip (either crash or successfully finish).
            ResetWaitAllCars();

            // Tell all cars to start at the same time
            Car.StartAllCars.Set();
        }
Beispiel #2
0
        public Wind(Car.ThreadingModel Model)
        {
            this.Model = Model;

            Random rnd = new Random();

            // Creates a number between 0 and 100
            Speed = rnd.Next(0, 101);

            // Randomly specify a wind direction
            WindDirection = (Direction)rnd.Next(0, 3);
        }