Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace temp1
- {
- using System;
- using System.Collections.Concurrent;
- using System.Collections.Generic;
- using System.Globalization;
- using System.Linq;
- using System.Net;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Media;
- using System.Windows.Shapes;
- using System.Windows.Threading;
- public class Product
- {
- public bool IsSoftwareUpdated = false;
- public bool IsProductInformationCorrect = false;
- public bool IsEOLProcessingCompleted = false;
- public Product() { }
- ~Product() { }
- }
- public class ProcessProduct
- {
- public readonly List<Product> listOfProducts = new List<Product>(new Product[10]);
- public readonly ConcurrentBag<Product> UnprocessedUnits = new ConcurrentBag<Product>();
- public readonly ConcurrentBag<Product> CurrentlyUpdating = new ConcurrentBag<Product>();
- public readonly ConcurrentBag<Product> CurrentlyVerifyingInfo = new ConcurrentBag<Product>();
- public readonly ConcurrentBag<Product> FinishedProcessing = new ConcurrentBag<Product>();
- private readonly DispatcherTimer _timer = new DispatcherTimer();
- public ProcessProduct()
- {
- _timer.Tick += Timer_Tick; //Every 1 second, call Timer_Tick
- _timer.Interval = new TimeSpan(0, 0, 1); //1 Second timer
- listOfProducts.ForEach(o => UnprocessedUnits.Add(o)); //Fill the UnprocessedUnits with all products
- StartProcessing();
- }
- private void StartProcessing()
- {
- _timer.Start();
- }
- private void Timer_Tick(object sender, EventArgs e)
- {
- ProductOrganizationHandler();
- foreach(Product prod in CurrentlyUpdating.ToList())
- {
- UpdateProcessHandler(prod); //Async function that uses await
- }
- foreach(Product prod in CurrentlyVerifyingInfo.ToList())
- {
- VerifyingInfoHandler(prod); //Async function that uses Await
- }
- }
- private void ProductOrganizationHandler()
- {
- for(int i = 0; i < UnprocessedUnits.Count; i++) //Move products to their initial processing state
- {
- if(!UnprocessedUnits.TryTake(out Product prod))
- continue;
- if(!prod.IsSoftwareUpdated)
- {
- CurrentlyUpdating.Add(prod);
- }
- else if(!prod.IsProductInformationCorrect)
- {
- CurrentlyVerifyingInfo.Add(prod);
- }
- else
- {
- FinishedProcessing.Add(prod);
- }
- }
- for(int i = 0; i < CurrentlyUpdating.Count; i++)
- {
- if(!CurrentlyUpdating.TryTake(out Product prod))
- continue;
- if(prod.IsSoftwareUpdated)
- {
- if(!prod.IsProductInformationCorrect)
- CurrentlyVerifyingInfo.Add(prod);
- else
- FinishedProcessing.Add(prod);
- }
- else
- {
- CurrentlyUpdating.Add(prod); //Put the product back in the bag if it hasn't finished
- }
- }
- for(int i = 0; i < CurrentlyVerifyingInfo.Count; i++)
- {
- if(!CurrentlyVerifyingInfo.TryTake(out Product prod))
- continue;
- if(prod.IsProductInformationCorrect)
- FinishedProcessing.Add(prod);
- else
- CurrentlyVerifyingInfo.Add(prod);
- }
- }
- private async void UpdateProcessHandler(Product prod)
- {
- await Task.Delay(1000).ConfigureAwait(false);
- //Does some actual work here and uses await
- }
- private async void VerifyingInfoHandler(Product prod)
- {
- await Task.Delay(1000).ConfigureAwait(false);
- //Does actual work here and communicates with the product via USB
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement