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(); }
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) { Thread th = Thread.CurrentThread; th.Name = "Primary Thread"; Console.WriteLine("{0} is executing Main()", Thread.CurrentThread.Name); Printer p = new Printer(); //return void and take no arguments //Thread secondary = new Thread(new ThreadStart(p.PrintNumbers)); //secondary.Name = "Secondary Thread"; //secondary.Start(); MessageBox.Show("busy"); AddParams ap = new AddParams(); ap.AddToParamsList(45); ap.AddToParamsList(86); Thread dependent = new Thread(new ParameterizedThreadStart(p.Add)); dependent.IsBackground = true; dependent.Start(ap); waitHandle.WaitOne(); Console.WriteLine("final output"); Console.ReadLine(); }