Vla_DOS

Mutex

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