按一下以給予評分及指教
MSDN
MSDN Library
.NET 開發
.NET Framework

  開啟低頻寬檢視
本頁僅適用於
Microsoft Visual Studio 2008/.NET Framework 3.5

其他版本也適用於下列軟體:
.NET Framework 類別庫
ThreadPool 類別

更新:2007 年 11 月

提供執行緒的集區,可用來公佈工作項目,處理非同步的 (Asynchronous) I/O,代表其他執行緒等候,和處理計時器。

命名空間:  System.Threading
組件:  mscorlib (在 mscorlib.dll 中)

Visual Basic (宣告)
<HostProtectionAttribute(SecurityAction.LinkDemand, Synchronization := True,  _
    ExternalThreading := True)> _
Public NotInheritable Class ThreadPool
Visual Basic (使用方式)
您不需宣告靜態類別的執行個體,就能存取其成員。
C#
[HostProtectionAttribute(SecurityAction.LinkDemand, Synchronization = true, 
    ExternalThreading = true)]
public static class ThreadPool
Visual C++
[HostProtectionAttribute(SecurityAction::LinkDemand, Synchronization = true, 
    ExternalThreading = true)]
public ref class ThreadPool abstract sealed
J#
/** @attribute HostProtectionAttribute(SecurityAction.LinkDemand, Synchronization = true, ExternalThreading = true) */
public final class ThreadPool
JScript
public final class ThreadPool
注意事項:

套用至這個型別或成員的 HostProtectionAttribute 屬性 (Attribute) 具有下列 Resources 屬性 (Property) 值:Synchronization | ExternalThreadingHostProtectionAttribute 不會影響桌面應用程式 (這些應用的啟動方式一般都是按兩下圖示、輸入命令或在瀏覽器輸入 URL)。如需詳細資訊,請參閱 HostProtectionAttribute 類別或 SQL Server 程式設計和主機保護屬性

許多應用程式所建立的執行緒把很多的時間花費在休眠狀態,等候著事件發生。也有的執行緒進入休眠狀態後只是定時甦醒,來輪詢變更或更新狀態的資訊。採取執行緒共用的方式,可將系統所管理的背景工作執行緒 (Worker Thread) 集區提供給您的應用程式,讓您的執行緒使用起來更有效率。一個執行緒監視著佇列在執行緒集區的數項等候作業的狀態。當有等候作業完成,執行緒集區的背景工作執行緒會執行對應的回呼函式 (Callback Function)。

注意事項:

Managed 執行緒集區中的執行緒為背景執行緒。也就是說,其 IsBackground 屬性為 true。這就表示,在所有前景執行緒都結束後,ThreadPool 執行緒並不會讓應用程式持續執行。

您也可以將與等候作業無關的工作項目佇列在執行緒集區。若要要求工作項目由執行緒集區中的執行緒處理,請呼叫 QueueUserWorkItem 方法。這個方法將執行緒集區中選出之執行緒將會呼叫之方法或委派 (Delegate) 的參考當做參數。工作項目進入佇列之後便無法取消。

計時器佇列中的計時器以及已登錄的等候作業也使用執行緒集區。它們的回呼函式佇列在執行緒集區。

每一個處理序都會有一個執行緒集區。執行緒集區的預設大小為每個可用處理器 25 個背景工作執行緒,以及 1000 個 I/O 完成執行緒。您可以使用 SetMaxThreads 方法變更執行緒集區中的執行緒數目。每個執行緒都使用預設的堆疊大小,並且按照預設的優先權來執行。

注意事項:

裝載 .NET Framework 的 Unmanaged 程式碼可以使用 mscoree.h 檔中所定義的 CorSetMaxThreads 函式變更執行緒集區的大小。

執行緒集區會維持最小閒置執行緒數目。針對背景工作執行緒,這個最小值的預設值為處理序的數目。GetMinThreads 方法會取得閒置背景工作和 I/O 完成執行緒的最小數目。

當所有執行緒集區都已指派至工作時,執行緒集區並不會立即開始建立新的閒置執行緒。為避免不必要地配置執行緒的堆疊空間,它會依照間隔建立新的閒置執行緒。這個間隔目前為半秒,不過在未來的 .NET Framework 版本中可能會變更。

如果應用程式受到突然增加的活動影響 (例如,大量佇列的執行緒集區工作),請使用 SetMinThreads 方法增加最小閒置執行緒的數目。否則,建立新閒置執行緒時的內建延遲可能會造成瓶頸。

注意事項:

不必要地增加閒置執行緒的數目也可能會造成效能問題。每個執行緒都必須配置堆疊空間。如果太多工作同時啟動,則所有工作可能都會變慢。找出正確的平衡才是效能調整的關鍵。

當執行緒集區重複使用執行緒時,不會清除執行緒區域儲存區或標示 ThreadStaticAttribute 屬性之欄位中的資料。因此,使用一種方法放入執行緒區域儲存區的資料,可能會對相同執行緒集區執行緒所執行的另一種方法公開。根據執行方法的執行緒集區執行緒而定,存取標示 ThreadStaticAttribute 屬性之欄位的方法可能會遇到不同的資料。

自 .NET Framework 2.0 版 Service Pack 1 開始,對於大量使用小型執行緒集區工作的應用程式而言,執行緒集區的處理量已大幅提升。這些應用程式將有以下三方面的改善:佇列工作、分派執行緒集區的執行緒,以及分派 I/O 完成執行緒。若要使用這項功能,您的應用程式應以 .NET Framework 3.5 版為目標。如需詳細資訊,請參閱 .NET Framework 3.5 架構

TopicLocation
HOW TO:建立非同步的 HTTP 處理常式建置 ASP .NET Web 應用程式
HOW TO:建立非同步的 HTTP 處理常式在 Visual Studio 中建立 ASP .NET Web 應用程式
Visual Basic
Imports System
Imports System.Threading

Public Class Example

    <MTAThread> _
    Public Shared Sub Main()
        ' Queue the task.
        ThreadPool.QueueUserWorkItem( _
            New WaitCallback(AddressOf ThreadProc) _
            )
        ' Note that you do not have to create the WaitCallback delegate
        ' explicitly in Visual Basic.  The following line also queues 
        ' the task:
        'ThreadPool.QueueUserWorkItem(AddressOf ThreadProc)

        Console.WriteLine("Main thread does some work, then sleeps.")
        ' If you comment out the Sleep, the main thread exits before
        ' the thread pool task runs.  The thread pool uses background
        ' threads, which do not keep the application running.  (This
        ' is a simple example of a race condition.)
        Thread.Sleep(1000)

        Console.WriteLine("Main thread exits.")
    End Sub

    ' This thread procedure performs the task.
    Shared Sub ThreadProc(stateInfo As Object)
        ' No state object was passed to QueueUserWorkItem, so 
        ' stateInfo is null.
        Console.WriteLine("Hello from the thread pool.")
    End Sub
End Class

C#
using System;
using System.Threading;
public class Example {
    public static void Main() {
        // Queue the task.
        ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc));

        Console.WriteLine("Main thread does some work, then sleeps.");
        // If you comment out the Sleep, the main thread exits before
        // the thread pool task runs.  The thread pool uses background
        // threads, which do not keep the application running.  (This
        // is a simple example of a race condition.)
        Thread.Sleep(1000);

        Console.WriteLine("Main thread exits.");
    }

    // This thread procedure performs the task.
    static void ThreadProc(Object stateInfo) {
        // No state object was passed to QueueUserWorkItem, so 
        // stateInfo is null.
        Console.WriteLine("Hello from the thread pool.");
    }
}

Visual C++
using namespace System;
using namespace System::Threading;
ref class Example
{
public:

   // This thread procedure performs the task.
   static void ThreadProc( Object^ stateInfo )
   {

      // No state object was passed to QueueUserWorkItem, so 
      // stateInfo is 0.
      Console::WriteLine( "Hello from the thread pool." );
   }

};

int main()
{

   // Queue the task.
   ThreadPool::QueueUserWorkItem( gcnew WaitCallback( Example::ThreadProc ) );
   Console::WriteLine( "Main thread does some work, then sleeps." );

   // If you comment out the Sleep, the main thread exits before
   // the thread pool task runs.  The thread pool uses background
   // threads, which do not keep the application running.  (This
   // is a simple example of a race condition.)
   Thread::Sleep( 1000 );
   Console::WriteLine( "Main thread exits." );
   return 0;
}


J#
import System.*;
import System.Threading.*;
import System.Threading.Thread;

public class Example
{
    public static void main(String[] args)
    {
        // Queue the task.
        ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc));
        Console.WriteLine("Main thread does some work, then sleeps.");

        // If you comment out the Sleep, the main thread exits before
        // the thread pool task runs.  The thread pool uses background
        // threads, which do not keep the application running.  (This
        // is a simple example of a race condition.)
        Thread.Sleep(1000);
        Console.WriteLine("Main thread exits.");
    } //main

    // This thread procedure performs the task.
    static void ThreadProc(Object stateInfo)
    {
        // No state object was passed to QueueUserWorkItem, so 
        // stateInfo is null.
        Console.WriteLine("Hello from the thread pool.");
    } //ThreadProc
} //Example

System..::.Object
  System.Threading..::.ThreadPool

此型別具備執行緒安全。

Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360

.NET Framework 和 .NET Compact Framework 並不支援各種平台的所有版本。如需支援平台版本的相關資訊,請參閱 .NET Framework 系統需求

.NET Framework

支援版本:3.5、3.0、2.0、1.1、1.0

.NET Compact Framework

支援版本:3.5、2.0、1.0

XNA Framework

支援版本:2.0、1.0
社群內容   什麼是社群內容?
新增內容 RSS  註解
Processing
© 2009 Microsoft Corporation. 著作權所有,並保留一切權利。 使用規定  |  商標  |  隱私權聲明
Page view tracker