public Shop() { Product iceCream = new Product("Ice Cream", 12); Product cake = new Product("Cake", 35); Product chocolate = new Product("Chocolate", 24); Stand standWithIceCreams = new Stand(iceCream); standWithIceCreams.OnWorkCompleted += Stand_OnWorkCompleted; Stand standWithCakes = new Stand(cake); standWithCakes.OnWorkCompleted += Stand_OnWorkCompleted; Stand standWithChocolates = new Stand(chocolate); standWithChocolates.OnWorkCompleted += Stand_OnWorkCompleted; _stands = new CustomLinkedList <Stand> { standWithIceCreams, standWithCakes, standWithChocolates }; _standsListLocker = new object(); _activeVisitorsCountLocker = new object(); _workCompleted = new EventWaitHandle(false, EventResetMode.ManualReset); }
private void Stand_OnWorkCompleted(object sender, EventArgs e) { Stand stand = sender as Stand; if (stand != null) { stand.OnWorkCompleted -= Stand_OnWorkCompleted; lock (_standsListLocker) { TotalProfit += stand.SelledProductsCount * stand.Product.Price; stand.ShowStatistic(); #if DEBUG ConsoleHelper.WriteInfo(String.Format("The stand with {0}s is closed.", stand.Product.Name)); #endif _stands.Remove(stand); if (_stands.Count == 0) { ConsoleHelper.WriteInfo("Shop is closed"); _workCompleted.Set(); } } } }