Threding

来源:互联网 发布:.erm如何生成数据库表 编辑:程序博客网 时间:2024/06/08 09:37
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;namespace _0526_3{    // Simple threading scenario: Start a static method running on a second thread    public class ThradExample    {        // The ThreadProc method is called when the thread starts.        // It loops ten times,writing to the console and yielding        // the rest of its time slice each time , and then end.        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);            }        }        public static void Main()        {            Console.WriteLine("Main thread: Start a second thread.");            // The constructor for the Thread class requires a ThreadStart            // delegate the represents the method to be executed on the thread.            // C# simplifies the creating of this delegate.            Thread t = new Thread(new ThreadStart(ThreadProc));            t.Start();            for (int i = 0; i < 4; i++)            {                Console.WriteLine("Main thread: Do some work.");                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();        }    }}
原创粉丝点击