InPowerS.Net

 找回密碼
 註冊
搜索
查看: 3074|回復: 0

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

[複製鏈接]
發表於 2009-6-18 12:58:27 | 顯示全部樓層 |閱讀模式
以前在使用VB來實現多執行緒的時候,發現有一定的難度。雖然也有這樣那樣的方法,但都不盡人意,但在C#中,要編寫多執行緒應用程式卻相當的簡單。這篇文章將作簡要的介紹。
    .NET將關於多執行緒的功能定義在System.Threading名字空間中。因此,要使用多執行緒,必須先聲明引用此名字空間(using System.Threading;)。
    即使你沒有編寫多執行緒應用程式的經驗,也可能聽說過“啟動執行緒”“殺死執行緒”這些詞,其實除了這兩個外,涉及多執行緒方面的還有諸如“暫停執行緒”“優先順序”“掛起執行緒”“恢復執行緒”等等。下面將一個一個的解釋。
a.啟動執行緒
    顧名思義,“啟動執行緒”就是新建並啟動一個執行緒的意思,如下代碼可實現:

  1.     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。具體實現例子如下:
  1.     thread.Priority = ThreadPriority.Highest;
複製代碼

e.掛起執行緒
    Thread類的Suspend方法用來掛起執行緒,知道調用Resume,此執行緒才可以繼續執行。如果執行緒已經掛起,那就不會起作用。

  1.     if (thread.ThreadState = ThreadState.Running)
  2.     {
  3.         thread.Suspend();
  4.     }
複製代碼
f.恢復執行緒
    用來恢復已經掛起的執行緒,以讓它繼續執行,如果執行緒沒掛起,也不會起作用。

  1.     if (thread.ThreadState = ThreadState.Suspended)
  2.     {
  3.         thread.Resume();
  4.     }
複製代碼
下面將列出一個例子,以說明簡單的執行緒功能。此例子來自于幫助文檔。

  1. using System;
  2. using System.Threading;
  3. // Simple threading scenario:Start a static method running on a second thread.
  4. public class ThreadExample
  5. {
  6.     // The ThreadProc method is called when the thread starts.
  7.     // It loops ten times, writing to the console and yielding
  8.     // the rest of its time slice each time, and then ends.
  9.     public static void ThreadProc()
  10.     {
  11.         for (int i = 0; i < 10; i++)
  12.         {
  13.             Console.WriteLine("ThreadProc: {0}", i);
  14.             // Yield the rest of the time slice.
  15.             Thread.Sleep(0);
  16.         }
  17.     }
  18.     public static void Main()
  19.     {
  20.         Console.WriteLine("Main thread: Start a second thread.");
  21.         // The constrUCtor for the Thread class requires a ThreadStart
  22.         // delegate that represents the method to be executed on the
  23.         // thread.C# simplifies the creation of this delegate.
  24.         Thread t = new Thread(new ThreadStart(ThreadProc));
  25.         // Start ThreadProc.On a uniprocessor, the thread does not get
  26.         // any processor time until the main thread yields.Uncomment
  27.         // the Thread.Sleep that follows t.Start() to see the difference.
  28.         t.Start();
  29.         //Thread.Sleep(0);
  30.         for (int i = 0; i < 4; i++)
  31.         {
  32.             Console.WriteLine("Main thread: Do some work.");
  33.             Thread.Sleep(0);
  34.         }
  35.         Console.WriteLine("Main thread: Call Join(), to wait until ThreadProc ends.");
  36.         t.Join();
  37.         Console.WriteLine("Main thread: ThreadProc.Join has returned.Press Enter to end program.");
  38.         Console.ReadLine();
  39.         }
  40. }
複製代碼
此代碼產生的輸出類似如下內容:
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.
您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則

小黑屋|Archiver|手機版|InPowerS.Net

GMT+8, 2025-4-11 22:04

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回復 返回頂部 返回列表