WaitCallback デリゲート
アセンブリ: mscorlib (mscorlib.dll 内)

/** @delegate */ /** @attribute ComVisibleAttribute(true) */ public delegate void WaitCallback ( Object state )

WaitCallback は、ThreadPool スレッドで実行するコールバック メソッドを表します。デリゲートを作成するには、WaitCallback コンストラクタにコールバック メソッドを渡します。メソッドには、ここに示すシグネチャを付ける必要があります。
実行するためのキューにタスクを置くには、WaitCallback デリゲートを ThreadPool.QueueUserWorkItem に渡します。コールバック メソッドは、スレッド プール スレッドが使用可能になったときに実行されます。
![]() |
---|
Visual Basic のユーザーは WaitCallback コンストラクタを省略できます。コールバック メソッドを QueueUserWorkItem に渡すときは単純に AddressOf 演算子を使用できます。Visual Basic は正しいデリゲート コンストラクタを自動的に呼び出します。 |
コールバック メソッドに情報を渡す場合は、必要な情報を格納したオブジェクトを作成し、実行するためのキューにタスクを置くときにこのオブジェクトを QueueUserWorkItem に渡します。コールバック メソッドが実行されるたびに、state パラメータはこのオブジェクトを保持します。
コールバックを使用してスレッド プール スレッドを同期する方法の詳細については、「マネージ スレッド プール」を参照してください。

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

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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- WaitCallback デリゲートのページへのリンク