Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- //using System.Threading.Tasks;
- namespace x
- { public struct Student
- {
- public string FullName { get; set; }
- public double Ball { get; set; }
- }
- public class StudentServices
- {
- Mutex mutexObj = new Mutex();
- Random random = new Random();
- public int Count { get; set; }
- static Student[] Students;
- public StudentServices(int Count)
- {
- this.Count = Count;
- Thread AThread = new Thread(new ThreadStart(Input));
- AThread.Name = $"Thread Input";
- Thread BThread = new Thread(new ThreadStart(Trunsver));
- BThread.Name = $"Thread Trunslate";
- Thread CThread = new Thread(new ThreadStart(Output));
- CThread.Name = $"Thread Output";
- //start input thread
- AThread.Start();
- Task.Delay(10);
- //translate date
- BThread.Start();
- Task.Delay(15);
- //output default date
- CThread.Start();
- Task.Delay(10);
- }
- public void Input()
- {
- mutexObj.WaitOne(); // зупиняєм потік для отримання мютекса
- Console.WriteLine($"{Thread.CurrentThread.Name} Start");
- Student student = new Student();
- Students = new Student[Count];
- for (int i = 0; i < Count; i++)
- {
- student.FullName = $"Name[{i + 1}]";
- student.Ball = random.Next(1, 100);
- Students[i] = student;
- }
- mutexObj.ReleaseMutex(); // звільняєм мютекс
- }
- public void Output()
- {
- mutexObj.WaitOne(); // зупиняєм потік для отримання мютекса
- Console.WriteLine($"{Thread.CurrentThread.Name} Start");
- if (Students != null)
- foreach (var item in Students)
- {
- Console.WriteLine("Name: " + item.FullName + ". Ball: " + item.Ball);
- }
- mutexObj.ReleaseMutex(); // // звільняєм мютекс
- }
- public void Trunsver()
- {
- mutexObj.WaitOne(); // зупиняєм потік для отримання мютекса
- Console.WriteLine($"{Thread.CurrentThread.Name} Start");
- for (int i = 0; i < Students.Length; i++)
- {
- if (Students[i].Ball < 30)
- {
- Students[i].Ball = 1;
- }
- else if (Students[i].Ball <= 60)
- {
- Students[i].Ball = 2;
- }
- else if (Students[i].Ball < 74)
- {
- Students[i].Ball = 3;
- }
- else if (Students[i].Ball < 90)
- {
- Students[i].Ball = 4;
- }
- else if (Students[i].Ball <= 100)
- {
- Students[i].Ball = 5;
- }
- }
- mutexObj.ReleaseMutexc(); // // звільняєм мютекс
- }
- }
- class Program
- {
- static void Main()
- {
- try
- {
- int size = 10;
- StudentServices student = new StudentServices(size);
- student.Input();
- student.Output();
- student.Trunsver();
- student.Output();
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment