Vla_DOS

Untitled

Nov 23rd, 2022 (edited)
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.77 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3. using System.Threading.Tasks;
  4.  
  5. namespace x
  6. {    public struct Student
  7.     {
  8.         public string FullName { get; set; }
  9.         public double Ball { get; set; }
  10.     }
  11.  
  12.     public class StudentServices
  13.     {
  14.         Mutex mutexObj = new Mutex();
  15.         Random random = new Random();
  16.         public int Count { get; set; }
  17.         static Student[] Students;
  18.         public StudentServices(int Count)
  19.         {
  20.             this.Count = Count;
  21.             Thread AThread = new Thread(new ThreadStart(Input));
  22.             AThread.Name = $"Thread Input";
  23.             Thread BThread = new Thread(new ThreadStart(Trunsver));
  24.             BThread.Name = $"Thread Trunslate";
  25.             Thread CThread = new Thread(new ThreadStart(Output));
  26.             CThread.Name = $"Thread Output";
  27.             //start input thread
  28.             AThread.Start();
  29.             Task.Delay(10);
  30.  
  31.             //translate date
  32.             BThread.Start();
  33.             Task.Delay(15);
  34.  
  35.             //output default date
  36.             CThread.Start();
  37.             Task.Delay(10);
  38.         }
  39.  
  40.         public void Input()
  41.         {
  42.             mutexObj.WaitOne();     // зупиняєм потік для отримання мютекса
  43.             Console.WriteLine($"{Thread.CurrentThread.Name} Start");
  44.             Student student = new Student();
  45.             Students = new Student[Count];
  46.             for (int i = 0; i < Count; i++)
  47.             {
  48.                 student.FullName = $"Name[{i + 1}]";
  49.                 student.Ball = random.Next(1, 100);
  50.                 Students[i] = student;
  51.             }
  52.             mutexObj.ReleaseMutex();    // звільняєм мютекс
  53.  
  54.         }
  55.  
  56.         public void Output()
  57.         {
  58.  
  59.             mutexObj.WaitOne();     // зупиняєм потік для отримання мютекса
  60.             Console.WriteLine($"{Thread.CurrentThread.Name} Start");
  61.             if (Students != null)
  62.                 foreach (var item in Students)
  63.                 {
  64.                     Console.WriteLine("Name: " + item.FullName + ". Ball: " + item.Ball);
  65.                 }
  66.             mutexObj.ReleaseMutex();    // // звільняєм мютекс
  67.  
  68.         }
  69.  
  70.         public void Trunsver()
  71.         {
  72.             mutexObj.WaitOne();     // зупиняєм потік для отримання мютекса
  73.             Console.WriteLine($"{Thread.CurrentThread.Name} Start");
  74.             for (int i = 0; i < Students.Length; i++)
  75.             {
  76.                 if (Students[i].Ball < 30)
  77.                 {
  78.                     Students[i].Ball = 1;
  79.                 }
  80.                 else if (Students[i].Ball <= 60)
  81.                 {
  82.                     Students[i].Ball = 2;
  83.                 }
  84.                 else if (Students[i].Ball < 74)
  85.                 {
  86.                     Students[i].Ball = 3;
  87.                 }
  88.                 else if (Students[i].Ball < 90)
  89.                 {
  90.                     Students[i].Ball = 4;
  91.                 }
  92.                 else if (Students[i].Ball <= 100)
  93.                 {
  94.                     Students[i].Ball = 5;
  95.                 }
  96.             }
  97.             mutexObj.ReleaseMutex();    // // звільняєм мютекс
  98.         }
  99.     }
  100.  
  101.     class Program
  102.     {
  103.         static void Main()
  104.         {
  105.             try
  106.             {
  107.                 int size = 10;
  108.                 StudentServices student = new StudentServices(size);
  109.                 student.Input();
  110.                 student.Output();
  111.                 student.Trunsver();
  112.                 student.Output();
  113.  
  114.             }
  115.             catch (Exception ex)
  116.             {
  117.                 Console.WriteLine(ex.Message);
  118.             }
  119.             Console.ReadKey();
  120.         }
  121.     }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment