static void Main(string[] args) { Console.WriteLine("***** The Amazing Thread App *****\n"); Console.Write("Do you want [1] or [2] threads? "); string threadCount = Console.ReadLine(); // Назначить имя текущему потоку. Thread primaryThread = Thread.CurrentThread; primaryThread.Name = "Primary"; // Вывести информацию о потоке. Console.WriteLine("-> {0} is executingMain()", Thread.CurrentThread.Name); // Создать рабочий класс. Printer p = new Printer(); switch (threadCount) { case "2": // Создать поток. Thread backgroundThread = new Thread(new ThreadStart(p.PrintNumbers)); backgroundThread.Name = "Secondary"; backgroundThread.Start(); break; case "1": p.PrintNumbers(); break; default: Console.WriteLine("I don't know what you want...you get 1 thread."); goto case "1"; // Для всех остальных вариантов ввода принимается один поток. } // Выполнить некоторую дополнительную работу. MessageBox.Show("I'm busy!", "Work on main thread..."); Console.ReadLine(); }
static void Main(string[] args) { Console.Write("Do you want [1] or [2] threads? "); string threadCount = Console.ReadLine(); Thread primaryThread = Thread.CurrentThread; primaryThread.Name = "Primary"; Console.WriteLine("-> {0} is executing Main()", Thread.CurrentThread.Name); Printer p = new Printer(); switch (threadCount) { case "2": Thread backgroundThread = new Thread(new ThreadStart(p.PrintNumbers)); backgroundThread.Name = "Secondary"; backgroundThread.Start(); break; case "1": p.PrintNumbers(); break; default: Console.WriteLine("I dont understand, using 1 thread"); goto case "1"; } MessageBox.Show("I'm busy", "Work on main thread..."); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** The Amazing Thread App *****\n"); Console.Write("Do you want [1] or [2] threads? "); string threadCount = Console.ReadLine(); // Name the current thread. Thread primaryThread = Thread.CurrentThread; primaryThread.Name = "Primary"; // Display Thread info. Console.WriteLine("-> {0} is executing Main()", Thread.CurrentThread.Name); // Make worker class. Printer p = new Printer(); switch (threadCount) { case "2": // Now make the thread. Thread backgroundThread = new Thread(new ThreadStart(p.PrintNumbers)); backgroundThread.Name = "Secondary"; backgroundThread.Start(); break; case "1": p.PrintNumbers(); break; default: Console.WriteLine("I don't know what you want...you get 1 thread."); goto case "1"; } // Do some additional work. System.Windows.Forms.MessageBox.Show("I'm busy!", "Work on main thread..."); Console.ReadLine(); }
private static void UseThreadStart() { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("=> The Amazing Thread App"); Console.Write("Do you want [1] or [2] threads?"); string threadCount = Console.ReadLine(); Thread primary = Thread.CurrentThread; primary.Name = "Primary"; Console.WriteLine($"-> {Thread.CurrentThread.Name} is executing primary"); Printer p = new Printer(); switch (threadCount) { case "2": Thread backgroud = new Thread(new ThreadStart(p.PrintNumbers)); backgroud.Name = "Secondary"; backgroud.Start(); break; case "1": p.PrintNumbers(); break; default: Console.WriteLine("I don't know what you want... y ou get 1 thread."); goto case "1"; } MessageBox.Show("I'm busy!", "Work on primary thread..."); }
static void Main(string[] args) { Console.WriteLine("***** The Amazing Thread App *****\n"); Console.Write("Do you want [1] or [2] threads? "); string threadCount = Console.ReadLine(); Thread primaryThread = Thread.CurrentThread; primaryThread.Name = "Primary"; Console.WriteLine("-> {0} is executing Main()", Thread.CurrentThread.Name); Printer p = new Printer(); switch (threadCount) { case "2": Thread backgroundThread = new Thread(new ThreadStart(p.PrintNumbers)); backgroundThread.Name = "Background"; backgroundThread.Start(); break; case "1": p.PrintNumbers(); break; default: Console.WriteLine("I don't know what you want... you get 1 thread."); goto case "1"; } MessageBox.Show("I'm busy!", "Work on main thread..."); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** The Amazing Thread App *****\n"); Console.Write("Do you want [1] or [2] threads?"); string threadCount = Console.ReadLine(); Thread primaryThread = Thread.CurrentThread; primaryThread.Name = "Primary"; Console.WriteLine("-> {0} is executing Main()", Thread.CurrentThread.Name); Printer p = new Printer(); switch (threadCount) { case "2": Thread backgroundThread = new Thread(new ThreadStart(p.PrintNumbers)); backgroundThread.Name = "Secondary"; backgroundThread.Start(); break; case "1": p.PrintNumbers(); break; default: Console.WriteLine("I don't know what you want ... you get 1 thread."); goto case "1"; } Console.WriteLine("This is on the main thread, and we are finished."); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** The Amazing Thread App *****\n"); Console.Write("Do you want [1] or [2] threads? "); string threadCount = Console.ReadLine(); // Name the current thread. Thread primaryThread = Thread.CurrentThread; primaryThread.Name = "Primary"; // Display Thread info. Console.WriteLine("-> {0} is executing Main()", Thread.CurrentThread.Name); //用于线程操作的“资源类” Printer p = new Printer(); // Make worker class.产生次线程 switch (threadCount) { case "2": // Now make the thread. /*Thread对象接受一个ParameterizedThreadStart(带参数)或ThreadStart(无参数)委托作为构造函数的参数 */ Thread backgroundThread = new Thread(new ThreadStart(p.PrintNumbers)); //ThreadStart委托:函数形式为void xxx(),无返回值,无参数 backgroundThread.Name = "Secondary"; backgroundThread.Start(); break; case "1": p.PrintNumbers(); break; default: Console.WriteLine("I don't know what you want...you get 1 thread."); goto case "1"; } // Do some additional work. MessageBox.Show("I'm busy!", "Work on main thread..."); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** The Amazing Thread App *****\n"); Console.Write("Do you want [1] or [2] threads? "); string threadCount = Console.ReadLine(); // Name the current thread. Thread primaryThread = Thread.CurrentThread; primaryThread.Name = "Primary"; // Display Thread info. Console.WriteLine("-> {0} is executing Main()", Thread.CurrentThread.Name); // Make worker class. Printer p = new Printer(); switch (threadCount) { case "2": // Now make the thread. Thread backgroundThread = new Thread(new ThreadStart(p.PrintNumbers)); backgroundThread.Name = "Secondary"; backgroundThread.Start(); //inform the CLR this thread is ready for processing break; case "1": p.PrintNumbers(); break; default: Console.WriteLine("I don't know what you want...you get 1 thread."); goto case "1"; } // Do some additional work. //Console.WriteLine("{0} will sleep for 6 m.", Thread.CurrentThread.Name); //Thread.Sleep(6000); // Message box will show when the 4th output. MessageBox.Show("I'm busy!", "Work on main thread..."); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** The Amazing Thread App *****\n"); Console.Write("Do you want [1] or [2] threads? "); string threadCount = Console.ReadLine(); // Name the current thread. Thread primaryThread = Thread.CurrentThread; primaryThread.Name = "Primary"; // Display Thread info. Console.WriteLine("-> {0} is executing Main()", Thread.CurrentThread.Name); // Make worker class. Printer p = new Printer(); #region Ask user how many threads they want switch (threadCount) { case "2": // Now make the thread. Thread backgroundThread = new Thread(new ThreadStart(p.PrintNumbers)); backgroundThread.Name = "Secondary"; backgroundThread.Start(); break; case "1": p.PrintNumbers(); break; default: Console.WriteLine("I don't know what you want...you get 1 thread."); goto case "1"; } #endregion // Do some additional work. MessageBox.Show("I'm busy!", "Work on main thread..."); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** The Amazing Thread App *****\n"); Console.Write("Do you want [1] or [2] threads? "); string threadCount = Console.ReadLine(); // Запрос количества потоков // Назначить имя текущему потоку. Thread pimaryThread = Thread.CurrentThread; pimaryThread.Name = "Primary"; // Вывести информацию о потоке. Console.WriteLine("-> {0} is executing Main ()", Thread.CurrentThread.Name); // Создать рабочий класс. Printer p = new Printer(); Thread bgroundThread = new Thread(new ThreadStart(p.PrintNumbers)); // Теперь это фоновый поток. bgroundThread.IsBackground = true; bgroundThread.Start(); switch (threadCount) { case "2": // Создать поток. Thread backgroundThread = new Thread(new ThreadStart(p.PrintNumbers)); backgroundThread.Name = "Secondary"; backgroundThread.Start(); break; case "1": p.PrintNumbers(); break; default: Console.WriteLine("I don't know what you want...you get 1 thread."); goto case "1"; // Переход к варианту с одним потоком } // Выполнить некоторую дополнительную работу. Console.Write("I'm busy!", "Work on main thread..."); Console.ReadLine(); }
static void Main() { WriteLine("***** The Amazing Thread App *****\n"); Write("Do you want [1] or [2] threads? "); string threadCount = ReadLine(); // Name the current thread. Thread primaryThread = CurrentThread; primaryThread.Name = "Primary"; // Display Thread info. WriteLine($"-> {CurrentThread.Name} is executing Main()"); // Make worker class. Printer p = new Printer(); switch (threadCount) { case "2": // Now make the thread. Thread backgroundThread = new Thread(new ThreadStart(p.PrintNumbers)) { Name = "Secondary" }; backgroundThread.Start(); break; case "1": p.PrintNumbers(); break; default: WriteLine("I don't know what you want...you get 1 thread."); goto case "1"; } // Do some additional work. Show("I'm busy!", "Work on main thread..."); ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** The Amazing Thread App *****\n"); Console.Write("Do you want [1] or [2] threads? "); string threadCount = Console.ReadLine(); // Name the current thread. Thread primaryThread = Thread.CurrentThread; primaryThread.Name = "Primary"; // Display Thread info. Console.WriteLine("-> {0} is executing Main()", Thread.CurrentThread.Name); //用于线程操作的“资源类” Printer p = new Printer(); // Make worker class.产生次线程 switch (threadCount) { case "2": // Now make the thread. /*Thread对象接受一个ParameterizedThreadStart(带参数)或ThreadStart(无参数)委托作为构造函数的参数 */ Thread backgroundThread = new Thread(new ThreadStart(p.PrintNumbers));//ThreadStart委托:函数形式为void xxx(),无返回值,无参数 backgroundThread.Name = "Secondary"; backgroundThread.Start(); break; case "1": p.PrintNumbers(); break; default: Console.WriteLine("I don't know what you want...you get 1 thread."); goto case "1"; } // Do some additional work. MessageBox.Show("I'm busy!", "Work on main thread..."); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("**** The Amazing Thread App ****"); Console.WriteLine("Do you want [1] or [2] threads?"); string threadCount = Console.ReadLine(); //Name the current thread Thread primaryThread = Thread.CurrentThread; primaryThread.Name = "Primary"; //Display thread info Console.WriteLine($"-> {Thread.CurrentThread.Name}"); //Make worker class Printer p = new Printer(); switch (threadCount) { case "2": //Now make the thread. Thread backgorundThread = n ew Thread(new ThreadStart(p.PrintNumbers)); backgorundThread.Name = "Secondary"; backgorundThread.Start(); break; case "1": p.PrintNumbers(); break; default: Console.WriteLine($"I don't know what you want... you get 1 thread..."); goto case "1"; } //Do some additional work MessageBox.Show("I'm busy!", "Work on main thread..."); Console.Read(); }
static void Main(string[] args) { Console.WriteLine("**** Fun with multi thread ****"); Console.WriteLine("Do you want 1 or 2 threads?"); string threadCount = Console.ReadLine(); Thread currThread = Thread.CurrentThread; currThread.Name = "Primary"; Console.WriteLine("{0} is executing Main()", Thread.CurrentThread.Name); Printer p = new Printer(); switch (threadCount) { case "2": { Thread backgroundThread = new Thread(new ThreadStart(p.PrintNumbers)); backgroundThread.Name = "Secondary"; backgroundThread.Start(); break; } case "1": { p.PrintNumbers(); break; } default: goto case "1"; } MessageBox.Show("I`m busy", "Work on main thread.."); Console.ReadLine(); }
static void Main(string[] args) { Thread.CurrentThread.Name = "Primary"; Console.WriteLine("How many threads do you want, 1 or 2"); int.TryParse(Console.ReadLine(), out int threads); Printer printer = new Printer(); switch (threads) { case 1: printer.PrintNumbers(); break; case 2: Thread sec = new Thread(new ThreadStart(printer.PrintNumbers)) { Name = "Secondary", Priority = ThreadPriority.Normal }; sec.Start(); break; case 3: Thread tert = new Thread(new ParameterizedThreadStart(printer.PrintNumbers2)) { Name = "Tertiary" }; tert.Start(new Data() { ia = new int[] { 2, 4, 6, 8, 10, 12 } }); break; default: goto case 1; }// end switch Console.WriteLine(); MessageBox.Show("Please i am busy on Thread: " + Thread.CurrentThread.Name); Console.ReadKey(); }
static void Main(string[] args) { Console.WriteLine("The Amazing Thread App"); Console.WriteLine("Do you want [1] or [2] threds? "); string threadsCount = Console.ReadLine(); //Назначить имя текущему потоку Thread primaryThread = Thread.CurrentThread; primaryThread.Name = "Primary"; //Вывести информацию о потоке Console.WriteLine($"-> {Thread.CurrentThread.Name} is executing Main()"); //Создать рабочий класс Printer p = new Printer(); switch (threadsCount) { case "2": //Создать поток Thread backgroundThread = new Thread(new ThreadStart(p.PrintNumbers)); backgroundThread.Name = "Secondary"; backgroundThread.Start(); break; case "1": p.PrintNumbers(); break; default: Console.WriteLine("I don't know what you want...you get 1 thread may be."); goto case "1"; } //Выполнить некоторую дополнительную работу MessageBox.Show("I'm busy!", "Work on main thread..."); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** The Amazing Thread App *****\n"); Console.Write($"Do you want [1] or [2] threads? "); string threadCount = Console.ReadLine(); //Name the current thread Thread primaryThread = Thread.CurrentThread; primaryThread.Name = "Primary"; //Display Thread info Console.WriteLine($"-> {Thread.CurrentThread.Name} is executing PrintNumbers()"); //Создание исполняющего класса (пункт 1 из чек листа) Printer p = new Printer(); switch (threadCount) { case "2": //Now make the thread Thread backgroundThread = new Thread(new ThreadStart(p.PrintNumbers)); backgroundThread.Name = "Secondary"; backgroundThread.Start(); break; case "1": p.PrintNumbers(); break; default: Console.WriteLine("I don't know what you want ... you get 1 thread"); goto case "1"; } //Do some additional work MessageBox.Show("I'm Busy!", "Work on main thread..."); Console.ReadLine(); }