예제 #1
0
        private static async Task _generateCustomers(Barber barber, DelayRange newCustomerTimeRange, DelayRange walkingTimeRange)
        {
            var customerId = 1;

            while (true)
            {
                await Task.Delay(newCustomerTimeRange.GetDelay());

                var customer = new Customer(customerId, walkingTimeRange);
                customer.SearchingForChairStarted += _customerIsSearchingForChairStarted;
                customerId++;

                await customer.LookAtBarber(barber);
            }
        }
예제 #2
0
        public async Task LookAtBarber(Barber barber)
        {
            if (barber.BarberState == BarberState.Sleeping)
            {
                await barber.AwakeBarber(this);
            }

            await Task.Delay(_walkingTimeRange.GetDelay());

            if (SearchingForChairStarted != null)
            {
                SearchingForChairStarted.Invoke(this,
                                                new SearchingForChairStartedEventArgs(this + " is searching for empty chair..."));
            }
        }
예제 #3
0
        static void Main(string[] args)
        {
            var walkingTimeRange = new DelayRange(20, 300);
            // 600-2000 for barbering time and 100-2900 allows to receive all possible events
            var barberingTimeRange   = new DelayRange(600, 2000);
            var newCustomerTimeRange = new DelayRange(100, 2900);

            var barber = new Barber(1, walkingTimeRange, barberingTimeRange);

            barber.FellAsleep              += _barberFellAsleep;
            barber.BarberingFinished       += _barberHasFinishedBarbering;
            barber.HasAwaken               += _barberHasAwaken;
            barber.InvititationBroadcasted += _barberHasBroadcastedInvititation;

            Task.Run(async() => await barber.Sleep());

            Task.Run(async() => await _generateCustomers(barber, newCustomerTimeRange, walkingTimeRange));

            Console.ReadKey();
        }