Vla_DOS

Untitled

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