congfeng02 发表于 2013-2-4 20:50:40

C#多线程简单易用原代码

using System;using System.Threading;class CRunThread{    static void Main()    {      Thread.CurrentThread.Name = "===================== Main Thread ==================";                CRunThread.PrintThreadInfo();      OperThreads();    }    public static void PrintThreadInfo()    {                //Thread.CurrentThread.Name = "The Main Thread" + rdm.Next();//当前线程的名称      Console.WriteLine(Thread.CurrentThread.Name + "\tStatus:" + Thread.CurrentThread.ThreadState);    }    public static void OperThreads()    {      Console.WriteLine("Thread Start / Stop / Join Sample");      A a = new A("hello, andylin:)");      ThreadStart srtTrd = new ThreadStart(a.Run);      Thread trd = new Thread(srtTrd);      trd.Start();                Thread.Sleep(10);      trd.Suspend();      //trd.Abort();      //trd.Join();      Console.WriteLine("\n ======== A.Run has finished ==========");      try      {            Console.WriteLine("Try to restart the A.Run...");            trd.Start();      }      catch (ThreadStateException e)      {            Console.WriteLine(e.ToString());      }      catch (System.Exception e)      {            Console.WriteLine(e.ToString());      }      Console.WriteLine("============ end of operator thread ==================");    }}class A{    private string m_strVal;      public A(string str)    {      m_strVal = str;    }    public void Run()    {      Thread.CurrentThread.Name = "The A Thread";//当前线程的名称      while (true)      {            CRunThread.PrintThreadInfo();            Console.WriteLine("strValue:" + m_strVal);            Console.WriteLine("A.Run is runing in its own thread...");      }    }}
页: [1]
查看完整版本: C#多线程简单易用原代码