/// <summary> /// Setup the buffer so the workers know what to produce. /// </summary> public void InitFoodItems() { FoodBuffer = new FoodItem[10]; FoodBuffer[0] = new FoodItem("Milk", 1f, 1.2f); FoodBuffer[1] = new FoodItem("Apple", .1f, .08f); FoodBuffer[2] = new FoodItem("Pear", .2f, .1f); FoodBuffer[3] = new FoodItem("Salad", .5f, 1); FoodBuffer[4] = new FoodItem("Sushi", 2, 1.5f); FoodBuffer[5] = new FoodItem("Bread", 1.2f, 1f); FoodBuffer[6] = new FoodItem("Wine", 1.1f, 1f); FoodBuffer[7] = new FoodItem("Hotdogs", .7f, 0.7f); FoodBuffer[8] = new FoodItem("Butter", 0.3f, 0.4f); FoodBuffer[9] = new FoodItem("Cheese", 1.6f, 0.2f); }
/// <summary> /// Store an item in the storage. /// </summary> /// <param name="item">Item to store</param> /// <returns>if the storage has room for it</returns> public bool DeliverItem(FoodItem item) { if (StorageBuffer.Count + 1 > MaxItems) { return false; } EmptySemaphore.WaitOne(); StorageMutex.WaitOne(); ProgressBar.InvokeMain(() => { ProgressBar.Value += (int)((1f / MaxItems) * 100); }); StorageBuffer.Enqueue(item); CountLabel.InvokeMain(() => { CountLabel.Text = (StorageBuffer.Count + "/" + MaxItems); }); StorageMutex.ReleaseMutex(); FullSemaphore.Release(); return true; }
/// <summary> /// Get the first available item in the storage. /// </summary> /// <param name="item">The first available item</param> /// <returns>If the storage found an item</returns> public bool FetchItem(out FoodItem item) { if (StorageBuffer.Count == 0) { item = default(FoodItem); return false; } FullSemaphore.WaitOne(); StorageMutex.WaitOne(); ProgressBar.InvokeMain(() => { ProgressBar.Value -= (int)((1f / MaxItems) * 100); }); item = StorageBuffer.Dequeue(); CountLabel.InvokeMain(() => { CountLabel.Text = (StorageBuffer.Count + "/" + MaxItems); }); StorageMutex.ReleaseMutex(); EmptySemaphore.Release(); return true; }