ThreadPool クラス
アセンブリ: mscorlib (mscorlib.dll 内)


![]() |
---|
このクラスに適用される HostProtectionAttribute 属性の Resources プロパティの値は、Synchronization または ExternalThreading です。HostProtectionAttribute は、デスクトップ アプリケーション (一般的には、アイコンをダブルクリック、コマンドを入力、またはブラウザに URL を入力して起動するアプリケーション) には影響しません。詳細については、HostProtectionAttribute クラスのトピックまたは「SQL Server プログラミングとホスト保護属性」を参照してください。 |
多くのアプリケーションが、イベント発生に備えて待機する時間の長いスレッド、つまりスリープ状態のスレッドを作成します。他のスレッドはスリープ状態に入り、ステータス情報の変更や更新がないかポーリングを行うサイクルのときの定期的に起動されます。スレッド プールを使用すると、システムで管理されるワーカー スレッドのプールをアプリケーションに提供することで、スレッドをより効率的に使用できます。1 つのスレッドが、スレッド プールのキューに置かれた複数の待機操作のステータスを監視します。待機操作が完了すると、スレッド プールのワーカー スレッドは該当するコールバック関数を実行します。
![]() |
---|
マネージ スレッド プールのスレッドは、バックグラウンド スレッドです。つまり、これらのスレッドの IsBackground プロパティは true です。したがって、ThreadPool のスレッドは、すべてのフォアグラウンド スレッドが終了した後は、アプリケーションの実行状態を維持しません。 |
待機操作に関連のない作業項目をスレッド プールのキューに置くこともできます。作業項目をスレッド プール内のスレッドで処理することを要求するには、QueueUserWorkItem メソッドを呼び出します。このメソッドは、スレッド プールから選択されたスレッドによって呼び出されるメソッドまたはデリゲートへの参照をパラメータとして取得します。キューに置かれた作業項目はキャンセルできません。
タイマ待ちタイマおよび登録された待機操作もスレッド プールを使用します。これらのコールバック関数は、スレッド プールのキューに置かれます。
プロセスごとに、1 つのスレッド プールがあります。スレッド プールの既定のサイズは、利用できるプロセッサごとに 25 個のスレッドです。スレッド プール内のスレッドの数は、SetMaxThreads メソッドを使用して変更できます。各スレッドは、既定のスタック サイズを使用し、既定の優先順位で実行されます。
![]() |
---|
.NET Framework をホストするアンマネージ コードで、mscoree.h ファイルで定義されている CorSetMaxThreads 関数を使用してスレッド プールのサイズを変更できます。 |

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
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."); } }
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; }
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.Threading.ThreadPool


Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


ThreadPool メソッド


ThreadPool メンバ
作業項目の送信、非同期 I/O の処理、他のスレッドの代理で行う待機、およびタイマの処理に使用できるスレッドのプールを提供します。
ThreadPool データ型で公開されるメンバを以下の表に示します。


Weblioに収録されているすべての辞書からThreadPoolを検索する場合は、下記のリンクをクリックしてください。

- ThreadPoolのページへのリンク