多线程(一)初步使用

 class Program6
    {
        public static void ThreadProc()
        {
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine("ThreadProc: {0}", i);
                // Yield the rest of the time slice.
                Thread.Sleep(0);
            }
        }
        static void Main(string[] args)
        {
            Console.WriteLine("Main thread: Start a second thread.");
            Thread t = new Thread(new ThreadStart(ThreadProc));
            t.Start();//子线程开始执行
            for (int i = 0; i < 4; i++)
            {
               
                Console.WriteLine("Main thread: Do some work.");
               // Console.WriteLine("Wait 1 second.");
                 Thread.Sleep(0);
            }
            Console.WriteLine("Main thread: Call Join(), to wait until ThreadProc ends.");
           
            t.Join(); //加这句话的意思是:先把子线程执行完,再执行本线程。
            Console.WriteLine("Main thread: ThreadProc.Join has returned.  Press Enter to end program.");
            Console.ReadLine();
        }
    }

结果:

猜你喜欢

转载自www.cnblogs.com/25miao/p/9862363.html
今日推荐