static void Main() { var baker = new CookieBakery(new BaseCookie()); var cookieBaker = new Thread(baker.BakeCookies); var customerList = new List <Customer> { new Customer("Fred"), new Customer("Ted"), new Customer("Greg") }; cookieBaker.Start(); foreach (var customer in customerList) { new Thread(customer.BuyCookie).Start(); } Thread.Sleep(100); while (baker.Running) { } foreach (var customer in customerList) { Console.WriteLine("{0} recieved a total of {1} cookie(s)", customer.GetName(), customer.PurchasedCookies); } Console.ReadKey(true); }
public void BuyCookie() { var attempts = 0; var maxAttempts = 40; var random = new Random(); var max = random.Next(); var time = new Stopwatch(); time.Start(); while (attempts < maxAttempts) { max = random.Next(1000, 1100); while (time.ElapsedMilliseconds < max) { } if (CookieBakery.SellToCustomer(this)) { PurchasedCookies++; } time.Restart(); attempts++; } }