Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading;
- #pragma warning disable 0618
- namespace Async_2
- {
- class Program
- {
- static Thread[] tArray = new Thread[10];
- static void Main(string[] args)
- {
- Program p = new Program();
- p.ThreadMain();
- }
- void ThreadMain()
- {
- InitAndDrawMenu();
- while (true)
- {
- try
- {
- string input = Console.ReadLine();
- if (input.Contains("start "))
- {
- if (input.Length == 7) // input = start x
- {
- int th = int.Parse(input.Substring(input.Length - 1, 1));
- Console.WriteLine("Starting {0}", tArray[th].Name);
- tArray[th].Resume();
- }
- else if (input.Length == 9) // input = "start x y" or "start x-y"
- {
- int i = 0;
- int th_last = int.Parse(input.Substring(input.Length - 1, 1));
- int th_first = int.Parse(input.Substring(input.Length - 3, 1));
- if (input.Substring(input.Length - 2, 1).Contains("-"))
- {
- while (i <= (th_last - th_first))
- {
- tArray[th_first + i].Resume();
- Console.WriteLine("Starting {0}", tArray[th_first + i].Name);
- i++;
- }
- }
- else
- {
- tArray[th_first].Resume();
- Console.WriteLine("Starting {0}", tArray[th_first].Name);
- tArray[th_last].Resume();
- Console.WriteLine("Starting {0}", tArray[th_last].Name);
- }
- }
- else
- {
- Console.WriteLine("Wrong input format, try again. Possible inputs:");
- Console.WriteLine("\t- start x\n\t- start x-y\n\t- start x y");
- }
- }
- else if (input.Contains("stop"))
- {
- bool[] running = new bool[(int)tArray.Length];
- int i = 0;
- foreach (Thread tab in tArray)
- {
- if (!tab.ThreadState.Equals(System.Threading.ThreadState.Suspended))
- {
- tab.Suspend();
- Console.WriteLine("Suspended {0}.", tab.Name);
- running[i] = true;
- }
- i++;
- }
- if (running.Any(item => item == false)) { Console.WriteLine("All threads are suspended"); }
- Console.Write("What to you want to do: ");
- }
- else if (input.Contains("exit"))
- {
- Environment.Exit(0);
- }
- else Console.WriteLine("There is no such input, try again");
- }
- catch (ThreadStateException) { }
- catch (FormatException) { Console.WriteLine("X and Y params must be numbers"); Console.Write("What to you want to do: "); }
- }
- }
- private static void ThreadProc(int thread)
- {
- try
- {
- Thread.CurrentThread.Suspend();
- int i = 0;
- while (i++ < 26)
- {
- Console.Write("{0}" + thread + " ", (char)(0x40 + i));
- Thread.Sleep(1000);
- }
- }
- catch (ThreadStateException) { }
- }
- private static void InitAndDrawMenu()
- {
- for (int i = 0; i < tArray.Count(); i++)
- {
- try
- {
- tArray[i] = new Thread(() => ThreadProc(i));
- tArray[i].Name = "thread_" + i;
- tArray[i].Start();
- Thread.Sleep(100);
- Console.WriteLine("{0}: {1}",tArray[i].Name, tArray[i].ThreadState);
- }
- catch (ThreadStateException) { }
- }
- Console.SetCursorPosition(45, 2);
- Console.Write("Possible options: (x and y are numbers)");
- Console.SetCursorPosition(50, 3);
- Console.Write("start x");
- Console.SetCursorPosition(50, 4);
- Console.Write("start x y");
- Console.SetCursorPosition(50, 5);
- Console.Write("start x-y");
- Console.SetCursorPosition(50, 6);
- Console.Write("stop");
- Console.SetCursorPosition(50, 7);
- Console.Write("exit");
- Console.SetCursorPosition(0, 12);
- Console.Write("Whad to you want to do: ");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement