Advertisement
Guest User

Async Resource Access and Claiming

a guest
Nov 3rd, 2021
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.98 KB | None | 0 0
  1. namespace temp1
  2. {
  3.     using System;
  4.     using System.Collections.Concurrent;
  5.     using System.Collections.Generic;
  6.     using System.Globalization;
  7.     using System.Linq;
  8.     using System.Net;
  9.     using System.Threading;
  10.     using System.Threading.Tasks;
  11.     using System.Windows;
  12.     using System.Windows.Controls;
  13.     using System.Windows.Media;
  14.     using System.Windows.Shapes;
  15.     using System.Windows.Threading;
  16.     public class Product
  17.     {
  18.         public bool IsSoftwareUpdated = false;
  19.         public bool IsProductInformationCorrect = false;
  20.         public bool IsEOLProcessingCompleted = false;
  21.  
  22.     public Product() { }
  23.      ~Product() { }
  24.     }
  25.  
  26. public class ProcessProduct
  27. {
  28.     public readonly List<Product> listOfProducts = new List<Product>(new Product[10]);
  29.  
  30.     public readonly ConcurrentBag<Product> UnprocessedUnits = new ConcurrentBag<Product>();
  31.     public readonly ConcurrentBag<Product> CurrentlyUpdating = new ConcurrentBag<Product>();
  32.     public readonly ConcurrentBag<Product> CurrentlyVerifyingInfo = new ConcurrentBag<Product>();
  33.     public readonly ConcurrentBag<Product> FinishedProcessing = new ConcurrentBag<Product>();
  34.  
  35.     private readonly DispatcherTimer _timer = new DispatcherTimer();
  36.  
  37.     public ProcessProduct()
  38.     {
  39.         _timer.Tick += Timer_Tick;                            //Every 1 second, call Timer_Tick
  40.         _timer.Interval = new TimeSpan(0, 0, 1);                //1 Second timer
  41.  
  42.         listOfProducts.ForEach(o => UnprocessedUnits.Add(o));  //Fill the UnprocessedUnits with all products
  43.  
  44.         StartProcessing();
  45.     }
  46.     private void StartProcessing()
  47.     {
  48.         _timer.Start();
  49.     }
  50.  
  51.     private void Timer_Tick(object sender, EventArgs e)
  52.     {
  53.         ProductOrganizationHandler();
  54.  
  55.         foreach(Product prod in CurrentlyUpdating.ToList())
  56.         {
  57.             UpdateProcessHandler(prod);  //Async function that uses await
  58.         }
  59.         foreach(Product prod in CurrentlyVerifyingInfo.ToList())
  60.         {
  61.             VerifyingInfoHandler(prod);  //Async function that uses Await
  62.         }
  63.     }
  64.  
  65.     private void ProductOrganizationHandler()
  66.     {
  67.         for(int i = 0; i < UnprocessedUnits.Count; i++)  //Move products to their initial processing state
  68.         {
  69.             if(!UnprocessedUnits.TryTake(out Product prod))
  70.                 continue;
  71.             if(!prod.IsSoftwareUpdated)
  72.             {
  73.                 CurrentlyUpdating.Add(prod);
  74.             }
  75.             else if(!prod.IsProductInformationCorrect)
  76.             {
  77.                 CurrentlyVerifyingInfo.Add(prod);
  78.             }
  79.             else
  80.             {
  81.                 FinishedProcessing.Add(prod);
  82.             }
  83.         }
  84.         for(int i = 0; i < CurrentlyUpdating.Count; i++)
  85.         {
  86.             if(!CurrentlyUpdating.TryTake(out Product prod))
  87.                 continue;
  88.  
  89.             if(prod.IsSoftwareUpdated)
  90.             {
  91.                 if(!prod.IsProductInformationCorrect)
  92.                     CurrentlyVerifyingInfo.Add(prod);
  93.                 else
  94.                     FinishedProcessing.Add(prod);
  95.             }
  96.             else
  97.             {
  98.                 CurrentlyUpdating.Add(prod); //Put the product back in the bag if it hasn't finished
  99.             }
  100.         }
  101.         for(int i = 0; i < CurrentlyVerifyingInfo.Count; i++)
  102.         {
  103.             if(!CurrentlyVerifyingInfo.TryTake(out Product prod))
  104.                 continue;
  105.  
  106.             if(prod.IsProductInformationCorrect)
  107.                 FinishedProcessing.Add(prod);
  108.             else
  109.                 CurrentlyVerifyingInfo.Add(prod);
  110.         }
  111.     }
  112.     private async void UpdateProcessHandler(Product prod)
  113.     {
  114.         await Task.Delay(1000).ConfigureAwait(false);
  115.         //Does some actual work here and uses await
  116.     }
  117.     private async void VerifyingInfoHandler(Product prod)
  118.     {
  119.         await Task.Delay(1000).ConfigureAwait(false);
  120.         //Does actual work here and communicates with the product via USB
  121.     }
  122. }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement