Advertisement
MagnusArias

PS1 | Asynchronous 2

Apr 25th, 2017
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.55 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading;
  8.  
  9. #pragma warning disable 0618
  10.  
  11. namespace Async_2
  12. {
  13.     class Program
  14.     {
  15.         static Thread[] tArray = new Thread[10];
  16.  
  17.  
  18.         static void Main(string[] args)
  19.         {
  20.            
  21.             Program p = new Program();
  22.             p.ThreadMain();
  23.          
  24.         }
  25.  
  26.         void ThreadMain()
  27.         {
  28.            
  29.             InitAndDrawMenu();
  30.  
  31.             while (true)
  32.             {
  33.                 try
  34.                 {
  35.                     string input = Console.ReadLine();
  36.                     if (input.Contains("start "))
  37.                     {
  38.                         if (input.Length == 7) // input = start x
  39.                         {
  40.                             int th = int.Parse(input.Substring(input.Length - 1, 1));
  41.                             Console.WriteLine("Starting {0}", tArray[th].Name);
  42.                             tArray[th].Resume();
  43.                         }
  44.                         else if (input.Length == 9) // input = "start x y" or "start x-y"
  45.                         {
  46.                             int i = 0;
  47.                             int th_last = int.Parse(input.Substring(input.Length - 1, 1));
  48.                             int th_first = int.Parse(input.Substring(input.Length - 3, 1));
  49.  
  50.                             if (input.Substring(input.Length - 2, 1).Contains("-"))
  51.                             {
  52.                                 while (i <= (th_last - th_first))
  53.                                 {
  54.                                     tArray[th_first + i].Resume();
  55.                                     Console.WriteLine("Starting {0}", tArray[th_first + i].Name);
  56.                                     i++;
  57.                                 }
  58.                             }
  59.                             else
  60.                             {
  61.                                 tArray[th_first].Resume();
  62.                                 Console.WriteLine("Starting {0}", tArray[th_first].Name);
  63.                                
  64.                                 tArray[th_last].Resume();
  65.                                 Console.WriteLine("Starting {0}", tArray[th_last].Name);
  66.                             }
  67.                         }
  68.                         else
  69.                         {
  70.                             Console.WriteLine("Wrong input format, try again. Possible inputs:");
  71.                             Console.WriteLine("\t- start x\n\t- start x-y\n\t- start x y");
  72.                         }
  73.                     }
  74.                     else if (input.Contains("stop"))
  75.                     {
  76.                         bool[] running = new bool[(int)tArray.Length];
  77.                         int i = 0;
  78.  
  79.                         foreach (Thread tab in tArray)
  80.                         {
  81.                             if (!tab.ThreadState.Equals(System.Threading.ThreadState.Suspended))
  82.                             {
  83.                                 tab.Suspend();
  84.                                 Console.WriteLine("Suspended {0}.", tab.Name);
  85.                                 running[i] = true;
  86.                             }
  87.                             i++;
  88.                         }
  89.                         if (running.Any(item => item == false)) { Console.WriteLine("All threads are suspended"); }
  90.  
  91.                         Console.Write("What to you want to do: ");
  92.                     }
  93.                     else if (input.Contains("exit"))
  94.                     {
  95.                         Environment.Exit(0);
  96.                     }
  97.                     else Console.WriteLine("There is no such input, try again");
  98.                 }
  99.                 catch (ThreadStateException) { }
  100.                 catch (FormatException) { Console.WriteLine("X and Y params must be numbers"); Console.Write("What to you want to do: "); }
  101.             }
  102.  
  103.         }
  104.         private static void ThreadProc(int thread)
  105.         {
  106.             try
  107.             {
  108.                 Thread.CurrentThread.Suspend();
  109.  
  110.                 int i = 0;
  111.                 while (i++ < 26)
  112.                 {
  113.                     Console.Write("{0}" + thread + " ", (char)(0x40 + i));
  114.                     Thread.Sleep(1000);
  115.                 }
  116.             }
  117.             catch (ThreadStateException) { }
  118.         }
  119.  
  120.         private static void InitAndDrawMenu()
  121.         {
  122.             for (int i = 0; i < tArray.Count(); i++)
  123.             {
  124.                 try
  125.                 {
  126.                     tArray[i] = new Thread(() => ThreadProc(i));
  127.                     tArray[i].Name = "thread_" + i;
  128.                     tArray[i].Start();
  129.                     Thread.Sleep(100);
  130.                     Console.WriteLine("{0}: {1}",tArray[i].Name, tArray[i].ThreadState);
  131.                 }
  132.                 catch (ThreadStateException) { }
  133.             }
  134.  
  135.             Console.SetCursorPosition(45, 2);
  136.             Console.Write("Possible options: (x and y are numbers)");
  137.             Console.SetCursorPosition(50, 3);
  138.             Console.Write("start x");
  139.             Console.SetCursorPosition(50, 4);
  140.             Console.Write("start x y");
  141.             Console.SetCursorPosition(50, 5);
  142.             Console.Write("start x-y");
  143.             Console.SetCursorPosition(50, 6);
  144.             Console.Write("stop");
  145.             Console.SetCursorPosition(50, 7);
  146.             Console.Write("exit");
  147.             Console.SetCursorPosition(0, 12);
  148.             Console.Write("Whad to you want to do: ");
  149.         }
  150.     }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement