次の方法で共有


ThreadPool.QueueUserWorkItem メソッド (WaitCallback)

メソッドを実行するためのキューに置きます。メソッドは、スレッド プールのスレッドが使用可能になったときに実行されます。

Overloads Public Shared Function QueueUserWorkItem( _
   ByVal callBack As WaitCallback _) As Boolean
[C#]
public static bool QueueUserWorkItem(WaitCallbackcallBack);
[C++]
public: static bool QueueUserWorkItem(WaitCallback* callBack);
[JScript]
public static function QueueUserWorkItem(
   callBack : WaitCallback) : Boolean;

パラメータ

  • callBack
    実行するメソッドを表す WaitCallback

戻り値

このメソッドが正常にキューに置かれた場合は true 。それ以外の場合は false

解説

キューに置かれたメソッドで必要となるデータは、メソッドを定義しているクラスのインスタンス フィールドに格納することもできますし、必要なデータを格納したオブジェクトを受け取る QueueUserWorkItem オーバーロードを使用することもできます。

[Visual Basic] メモ   Visual Basic のユーザーは WaitCallback コンストラクタを省略できます。コールバック メソッドを QueueUserWorkItem に渡すときは単純に AddressOf 演算子を使用できます。Visual Basic は正しいデリゲート コンストラクタを自動的に呼び出します。

使用例

 
Imports System
Imports System.Threading

Public Class Example
    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.");
    }
}

[C++] 
#using <mscorlib.dll>

using namespace System;
using namespace System::Threading;

__gc 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(S"Hello from the thread pool.");
    }
};

int main() 
{
    // Queue the task.
    ThreadPool::QueueUserWorkItem(new WaitCallback(0, Example::ThreadProc));

    Console::WriteLine(S"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(S"Main thread exits.");
    return 0;
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET

参照

ThreadPool クラス | ThreadPool メンバ | System.Threading 名前空間 | ThreadPool.QueueUserWorkItem オーバーロードの一覧 | スレッド プーリング