1. 程式人生 > >多線程(一)初步使用

多線程(一)初步使用

spa ret art turn adl res ring cond 技術分享

 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(); } }

結果:

技術分享圖片

多線程(一)初步使用