C# Thread类线程常用操作

创建线程

线程是通过扩展 Thread 类创建的。扩展的 Thread 类调用 Start() 方法来开始子线程的执行。

下面的程序演示了这个概念:.

 class ThreadCreationProgram    {        public static void CallToChildThread()        {            Console.WriteLine("Child thread starts");        }               static void Main(string[] args)        {            ThreadStart childref = new ThreadStart(CallToChildThread);            Console.WriteLine("In Main: Creating the Child thread");            Thread childThread = new Thread(childref);            childThread.Start();            Console.ReadKey();        }    }

当上面的代码被编译和执行时,它会产生下列结果:

In Main: Creating the Child threadChild thread starts

管理线程

Thread 类提供了各种管理线程的方法。

下面的实例演示了 sleep() 方法的使用,用于在一个特定的时间暂停线程。

class ThreadCreationProgram    {        public static void CallToChildThread()        {            Console.WriteLine("Child thread starts");            // 线程暂停 5000 毫秒            int sleepfor = 5000;            Console.WriteLine("Child Thread Paused for {0} seconds",                              sleepfor / 1000);            Thread.Sleep(sleepfor);            Console.WriteLine("Child thread resumes");        }               static void Main(string[] args)        {            ThreadStart childref = new ThreadStart(CallToChildThread);            Console.WriteLine("In Main: Creating the Child thread");            Thread childThread = new Thread(childref);            childThread.Start();            Console.ReadKey();        }    }

当上面的代码被编译和执行时,它会产生下列结果:

In Main: Creating the Child threadChild thread startsChild Thread Paused for 5 secondsChild thread resumes

销毁线程

Abort() 方法用于销毁线程。

通过抛出 threadabortexception 在运行时中止线程。这个异常不能被捕获,如果有 finally 块,控制会被送至 finally 块。

下面的程序说明了这点:

class ThreadCreationProgram    {        public static void CallToChildThread()        {            try            {
                Console.WriteLine("Child thread starts");                // 计数到 10                for (int counter = 0; counter <= 10; counter++)                {                    Thread.Sleep(500);                    Console.WriteLine(counter);                }                Console.WriteLine("Child Thread Completed");
            }            catch (ThreadAbortException e)            {                Console.WriteLine("Thread Abort Exception");            }            finally            {                Console.WriteLine("Couldn't catch the Thread Exception");            }
        }               static void Main(string[] args)        {            ThreadStart childref = new ThreadStart(CallToChildThread);            Console.WriteLine("In Main: Creating the Child thread");            Thread childThread = new Thread(childref);            childThread.Start();            // 停止主线程一段时间            Thread.Sleep(2000);            // 现在中止子线程            Console.WriteLine("In Main: Aborting the Child thread");            childThread.Abort();            Console.ReadKey();        }    }

当上面的代码被编译和执行时,它会产生下列结果:

In Main: Creating the Child threadChild thread starts012In Main: Aborting the Child threadThread Abort ExceptionCouldn't catch the Thread Exception