Ejemplo n.º 1
0
        public static void ThreadDemoSix()
        {
            Console.WriteLine("***** Fun with the CLR Thread Pool *****\n");
            Console.WriteLine("ID of thread in Main(): {0}", Thread.CurrentThread.ManagedThreadId);

            PrinterBll   p        = new PrinterBll();
            WaitCallback workItem = new WaitCallback(PrintTheNumbers);

            for (int i = 0; i < 10; i++)
            {
                ThreadPool.QueueUserWorkItem(workItem, p);
            }
            Console.WriteLine("All task quequed");
            Console.ReadLine();
        }
Ejemplo n.º 2
0
        public static void ThreadDemoFour()
        {
            Console.WriteLine("***** Synchronizing Threads *****\n");
            PrinterBll p = new PrinterBll();

            Thread[] threads = new Thread[10];
            for (int i = 0; i < 10; i++)
            {
                threads[i]      = new Thread(new ThreadStart(p.PrintNumbers));
                threads[i].Name = string.Format("worker thread #{0}", i);
            }
            foreach (var t in threads)
            {
                t.Start();
            }
            Console.ReadLine();
        }
Ejemplo n.º 3
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();
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 灵活使用
        /// </summary>
        /// <param name="state"></param>
        static void PrintTheNumbers(object state)
        {
            PrinterBll task = (PrinterBll)state;

            task.PrintNumbers();
        }