菜鸟 也谈谈多线程

做开发快一年了,大部分都是有的dan线程,也从来没有考虑去去多线程处理一些东西。今天突然 看到 petshop的多线程 代码就贴出来吧

 static void Main() {


            Thread workTicketThread;
            Thread[] workerThreads = new Thread[threadCount];


            for (int i = 0; i < threadCount; i++) {


                workTicketThread = new Thread(new ThreadStart(ProcessOrders));


                // Make this a background thread, so it will terminate when the main thread/process is de-activated
                workTicketThread.IsBackground = true;
                workTicketThread.SetApartmentState(ApartmentState.STA);


                // Start the Work
                workTicketThread.Start();
                workerThreads[i] = workTicketThread;
            }


            Console.WriteLine("Processing started. Press Enter to stop.");
            Console.ReadLine();
            Console.WriteLine("Aborting Threads. Press wait...");


            //abort all threads
            for (int i = 0; i < workerThreads.Length; i++) {


                workerThreads[i].Abort();
            }


            Console.WriteLine();
            Console.WriteLine(totalOrdersProcessed + " Orders processed.");
            Console.WriteLine("Processing stopped. Press Enter to exit.");
            Console.ReadLine();
        }

posted @ 2011-11-07 22:52  likunran  阅读(177)  评论(0编辑  收藏  举报