熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

Visual C#中編寫多線程程序之起步

2022-06-13   來源: .NET編程 

  net將關於多線程的功能定義在SystemThreading名字空間中因此要使用多線程必須先聲明引用此名字空間(using SystemThreading;)

  即使你沒有編寫多線程應用程序的經驗也可能聽說過啟動線程殺死線程這些詞其實除了這兩個外涉及多線程方面的還有諸如暫停線程優先級掛起線程恢復線程等等下面將一個一個的解釋

  a啟動線程

  顧名思義啟動線程就是新建並啟動一個線程的意思如下代碼可實現

Thread thread = new Thread(new ThreadStart( Count));

  其中的 Count 是將要被新線程執行的函數

  b殺死線程

  殺死線程就是將一線程斬草除根為了不白費力氣在殺死一個線程前最好先判斷它是否還活著(通過 IsAlive 屬性)然後就可以調用 Abort 方法來殺死此線程

  c暫停線程

  它的意思就是讓一個正在運行的線程休眠一段時間如 threadSleep(); 就是讓線程休眠秒鐘

  d優先級

  這個用不著解釋了Thread類中有一個ThreadPriority屬性它用來設置優先級但不能保證操作系統會接受該優先級一個線程的優先級可 分為Normal AboveNormal BelowNormal Highest Lowest具體實現例子如下:

threadPriority = ThreadPriorityHighest;

  e掛起線程

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

  if (threadThreadState = ThreadStateRunning)
  {
   threadSuspend();
  } 

  f恢復線程

  用來恢復已經掛起的線程以讓它繼續執行如果線程沒掛起也不會起作用

  if (threadThreadState = ThreadStateSuspended)
  {
   threadResume();
  }

  下面將列出一個例子以說明簡單的線程處理功能此例子來自於幫助文檔

  using System;
  using SystemThreading;
  // 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 = ; i < ; i++)
    {
     ConsoleWriteLine(ThreadProc: {} i);
     // Yield the rest of the time slice
     ThreadSleep();
    }
   }
   public static void Main()
   {
    ConsoleWriteLine(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 ThreadSleep that follows tStart() to see the difference
    tStart();
    //ThreadSleep();
    for (int i = ; i < ; i++)
    {
     ConsoleWriteLine(Main thread: Do some work);
     ThreadSleep();
    }
    ConsoleWriteLine(Main thread: Call Join() to wait until ThreadProc ends);
    tJoin();
    ConsoleWriteLine(Main thread: ThreadProcJoin has returned Press Enter to end program);
    ConsoleReadLine();
   }
  } 

  此代碼產生的輸出類似如下內容

  Main thread: Start a second thread
  Main thread: Do some work
  ThreadProc:
  Main thread: Do some work
  ThreadProc:
  Main thread: Do some work
  ThreadProc:
  Main thread: Do some work
  ThreadProc:
  Main thread: Call Join() to wait until ThreadProc ends
  ThreadProc:
  ThreadProc:
  ThreadProc:
  ThreadProc:
  ThreadProc:
  ThreadProc:
  Main thread: ThreadProcJoin has returned Press Enter to end program


From:http://tw.wingwit.com/Article/program/net/201311/14673.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.