次の方法で共有


ThreadPool.QueueUserWorkItem メソッド

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

オーバーロードの一覧

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

.NET Compact Framework でもサポート。

[Visual Basic] Overloads Public Shared Function QueueUserWorkItem(WaitCallback) As Boolean

[C#] public static bool QueueUserWorkItem(WaitCallback);

[C++] public: static bool QueueUserWorkItem(WaitCallback*);

[JScript] public static function QueueUserWorkItem(WaitCallback) : Boolean;

実行するためのキューにメソッドを置き、そのメソッドが使用するデータを含んだオブジェクトを指定します。メソッドは、スレッド プールのスレッドが使用可能になったときに実行されます。

.NET Compact Framework でもサポート。

[Visual Basic] Overloads Public Shared Function QueueUserWorkItem(WaitCallback, Object) As Boolean

[C#] public static bool QueueUserWorkItem(WaitCallback, object);

[C++] public: static bool QueueUserWorkItem(WaitCallback*, Object*);

[JScript] public static function QueueUserWorkItem(WaitCallback, Object) : Boolean;

使用例

[Visual Basic, C#, C++] メモ   ここでは、QueueUserWorkItem のオーバーロード形式のうちの 1 つだけについて、使用例を示します。その他の例については、各オーバーロード形式のトピックを参照してください。

 
' This example shows how to create an object containing task
' information, and pass that object to a task queued for
' execution by the thread pool.
Imports System
Imports System.Threading
' TaskInfo holds state information for a task that will be
' executed by a ThreadPool thread.
Public Class TaskInfo
    ' State information for the task.  These members
    ' can be implemented as read-only properties, read/write
    ' properties with validation, and so on, as required.
    Public Boilerplate As String
    Public Value As Integer

    ' Public constructor provides an easy way to supply all
    ' the information needed for the task.
    Public Sub New(text As String, number As Integer)
        Boilerplate = text
        Value = number
    End Sub
End Class

Public Class Example
    Public Shared Sub Main()
        ' Create an object containing the information needed
        ' for the task.
        Dim ti As New TaskInfo("This report displays the number {0}.", 42)

        ' Queue the task and data.
        If ThreadPool.QueueUserWorkItem( _
            New WaitCallback(AddressOf ThreadProc), ti) Then
        
            Console.WriteLine("Main thread does some work, then sleeps.")

            ' If you comment out the Sleep, the main thread exits before
            ' the ThreadPool task has a chance to run.  ThreadPool 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.")
        Else
            Console.WriteLine("Unable to queue ThreadPool request.")
        End If
    End Sub

    ' The thread procedure performs the independent task, in this case
    ' formatting and printing a very simple report.
    '
    Shared Sub ThreadProc(stateInfo As Object)
        Dim ti As TaskInfo = CType(stateInfo, TaskInfo)
        Console.WriteLine(ti.Boilerplate, ti.Value)
    End Sub
End Class

[C#] 
// This example shows how to create an object containing task
// information, and pass that object to a task queued for
// execution by the thread pool.
using System;
using System.Threading;

// TaskInfo holds state information for a task that will be
// executed by a ThreadPool thread.
public class TaskInfo {
    // State information for the task.  These members
    // can be implemented as read-only properties, read/write
    // properties with validation, and so on, as required.
    public string Boilerplate;
    public int Value;

    // Public constructor provides an easy way to supply all
    // the information needed for the task.
    public TaskInfo(string text, int number) {
        Boilerplate = text;
        Value = number;
    }
}

public class Example {
    public static void Main() {
        // Create an object containing the information needed
        // for the task.
        TaskInfo ti = new TaskInfo("This report displays the number {0}.", 42);

        // Queue the task and data.
        if (ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc), ti)) {    
            Console.WriteLine("Main thread does some work, then sleeps.");

            // If you comment out the Sleep, the main thread exits before
            // the ThreadPool task has a chance to run.  ThreadPool 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.");
        }
        else {
            Console.WriteLine("Unable to queue ThreadPool request."); 
        }
    }

    // The thread procedure performs the independent task, in this case
    // formatting and printing a very simple report.
    //
    static void ThreadProc(Object stateInfo) {
        TaskInfo ti = (TaskInfo) stateInfo;
        Console.WriteLine(ti.Boilerplate, ti.Value); 
    }
}

[C++] 
// This example shows how to create an Object* containing task
// information, and pass that Object* to a task queued for
// execution by the thread pool.
#using <mscorlib.dll>

using namespace System;
using namespace System::Threading;

// TaskInfo holds state information for a task that will be
// executed by a ThreadPool thread.
public __gc class TaskInfo 
{
    // State information for the task.  These members
    // can be implemented as read-only properties, read/write
    // properties with validation, and so on, as required.
public:
    String* Boilerplate;
    int Value;

    // Public constructor provides an easy way to supply all
    // the information needed for the task.
    TaskInfo(String* text, int number) 
    {
        Boilerplate = text;
        Value = number;
    }
};

public __gc struct Example
{
    // The thread procedure performs the independent task, in this case
    // formatting and printing a very simple report.
    //
    static void ThreadProc(Object* stateInfo) 
    {
        TaskInfo* ti = dynamic_cast<TaskInfo*>(stateInfo);
        Console::WriteLine(ti->Boilerplate, __box(ti->Value)); 
    }
};

int main() 
{
    // Create an object containing the information needed
    // for the task.
    TaskInfo* ti = new TaskInfo(S"This report displays the number {0}.", 42);

    // Queue the task and data.
    if (ThreadPool::QueueUserWorkItem(new WaitCallback(0, Example::ThreadProc), ti)) 
    {    
        Console::WriteLine(S"Main thread does some work, then sleeps.");

        // If you comment out the Sleep, the main thread exits before
        // the ThreadPool task has a chance to run.  ThreadPool 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.");
    }
    else 
    {
        Console::WriteLine(S"Unable to queue ThreadPool request."); 
    }
    return 0;
}

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

参照

ThreadPool クラス | ThreadPool メンバ | System.Threading 名前空間