private bool BigSorting() { try { bool res; // запуск пишущего потока writer = Factories.CreateBigSortWriter(QManager); writer.StartThread(); for (int i = 0; i < threadscount; i++) { // запуск рабочих потокв threads[i] = Factories.CreateBigSortThread(QManager); threads[i].StartThread(); } // запуск чтения reader = Factories.CreateBigSortReader(QManager, args[0]); reader.StartThread(); // ждем потоки res = QManager.Wait(); if (!res) { throw new Exception(); } writer.JoinThread(); return(res); } catch { /// прерывание всех потоков reader.AbortThread(); writer.AbortThread(); for (int j = 0; j < threadscount; j++) { if (threads[j] != null) { threads[j].AbortThread(); threads[j].JoinThread(); } } reader.JoinThread(); writer.JoinThread(); return(false); } }
static void Main(string[] args) { if (args.Length == 3 && File.Exists(args[0])) { // use this property or Console.CancelKeyPress event to handle Ctrl+c Console.TreatControlCAsInput = true; // Stopwatch Stopwatch sw = new Stopwatch(); sw.Start(); //GZipStream IBigSortThread bigsort = Factories.CreateBigSortManager(args); Console.WriteLine("BigSort is working..."); bigsort.StartThread(); // waiting for finish or ctrl+c Console.WriteLine("Press Ctrl+c to exit"); ConsoleKeyInfo cki = new ConsoleKeyInfo(); while (!bigsort.IsDone()) { if (Console.KeyAvailable) { cki = Console.ReadKey(true); if ((cki.Key == ConsoleKey.C) && (cki.Modifiers & ConsoleModifiers.Control) != 0) { bigsort.AbortThread(); bigsort.JoinThread(); break; } } } Console.WriteLine("BigSort has done."); sw.Stop(); Console.WriteLine("Time elapsed: {0}", sw.Elapsed); if (!bigsort.ResultOK()) { Console.WriteLine("Error code = 1"); Environment.Exit(1); } } else { Console.WriteLine("Command line parameters error."); Console.WriteLine("The right way:"); Console.WriteLine("BigSort.exe <source file> <destination file> <free RAM (Mb)>"); } }