〥羽〥 發表於 2009-6-18 12:58:27

在C#中編寫多執行緒應用程式

以前在使用VB來實現多執行緒的時候,發現有一定的難度。雖然也有這樣那樣的方法,但都不盡人意,但在C#中,要編寫多執行緒應用程式卻相當的簡單。這篇文章將作簡要的介紹。
    .NET將關於多執行緒的功能定義在System.Threading名字空間中。因此,要使用多執行緒,必須先聲明引用此名字空間(using System.Threading;)。
    即使你沒有編寫多執行緒應用程式的經驗,也可能聽說過“啟動執行緒”“殺死執行緒”這些詞,其實除了這兩個外,涉及多執行緒方面的還有諸如“暫停執行緒”“優先順序”“掛起執行緒”“恢復執行緒”等等。下面將一個一個的解釋。
a.啟動執行緒
    顧名思義,“啟動執行緒”就是新建並啟動一個執行緒的意思,如下代碼可實現:
    Thread thread1 = new Thread(new ThreadStart( Count));
    其中的 Count 是將要被新執行緒執行的函數。
b.殺死執行緒
    “殺死執行緒”就是將一執行緒斬草除根,為了不白費力氣,在殺死一個執行緒前最好先判斷它是否還活著(通過 IsAlive 屬性),然後就可以調用 Abort 方法來殺死此執行緒。
c.暫停執行緒
    它的意思就是讓一個正在運行的執行緒休眠一段時間。如thread.Sleep(1000); 就是讓執行緒休眠1秒鐘。
d.優先順序
    這個用不著解釋了。Thread類中有一個ThreadPriority屬性,它用來設置優先順序,但不能保證作業系統會接受該優先順序。一個執行緒的優先順序可分為5種:Normal, AboveNormal, BelowNormal, Highest, Lowest。具體實現例子如下:
    thread.Priority = ThreadPriority.Highest;
e.掛起執行緒
    Thread類的Suspend方法用來掛起執行緒,知道調用Resume,此執行緒才可以繼續執行。如果執行緒已經掛起,那就不會起作用。
    if (thread.ThreadState = ThreadState.Running)
    {
      thread.Suspend();
    }f.恢復執行緒
    用來恢復已經掛起的執行緒,以讓它繼續執行,如果執行緒沒掛起,也不會起作用。
    if (thread.ThreadState = ThreadState.Suspended)
    {
      thread.Resume();
    }下面將列出一個例子,以說明簡單的執行緒功能。此例子來自于幫助文檔。
using System;
using System.Threading;
// Simple threading scenario:Start a static method running on a second thread.
public class ThreadExample
{
    // 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 ends.
    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 that represents the method to be executed on the
      // thread.C# simplifies the creation of this delegate.
      Thread t = new Thread(new ThreadStart(ThreadProc));
      // Start ThreadProc.On a uniprocessor, the thread does not get
      // any processor time until the main thread yields.Uncomment
      // the Thread.Sleep that follows t.Start() to see the difference.
      t.Start();
      //Thread.Sleep(0);
      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();
      }
}此代碼產生的輸出類似如下內容:
Main thread: Start a second thread.
Main thread: Do some work.
ThreadProc: 0
Main thread: Do some work.
ThreadProc: 1
Main thread: Do some work.
ThreadProc: 2
Main thread: Do some work.
ThreadProc: 3
Main thread: Call Join(), to wait until ThreadProc ends.
ThreadProc: 4
ThreadProc: 5
ThreadProc: 6
ThreadProc: 7
ThreadProc: 8
ThreadProc: 9
Main thread: ThreadProc.Join has returned.Press Enter to end program.
頁: [1]
查看完整版本: 在C#中編寫多執行緒應用程式