예제 #1
0
        public static void ThreadDemoTwo()
        {
            Console.WriteLine("***** the Amazing Thread App *****\n");
            Console.Write("Do you need [1] or [2] threads?  ");
            string threadCount = Console.ReadLine();

            //命名当前线程
            Thread primaryThread = Thread.CurrentThread;

            primaryThread.Name = "Primary";

            //显示进程的信息
            Console.WriteLine("->{0} is executing Main()", Thread.CurrentThread.Name);

            PrinterBll p = new PrinterBll();

            switch (threadCount)
            {
            case "2":
                //设置线程
                Thread backgroudThread = new Thread(new ThreadStart(p.PrintNumbers));
                backgroudThread.Name = "Secondary";
                backgroudThread.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();
        }
예제 #2
0
        /// <summary>
        /// 灵活使用
        /// </summary>
        /// <param name="state"></param>
        static void PrintTheNumbers(object state)
        {
            PrinterBll task = (PrinterBll)state;

            task.PrintNumbers();
        }