Export (0) Print
Expand All

ThreadPool.QueueUserWorkItem Method (WaitCallback, Object)

.NET Framework 1.1

Queues a method for execution, and specifies an object containing data to be used by the method. The method executes when a thread pool thread becomes available.

[Visual Basic]
Overloads Public Shared Function QueueUserWorkItem( _
   ByVal callBack As WaitCallback, _
   ByVal state As Object _
) As Boolean
[C#]
public static bool QueueUserWorkItem(
 WaitCallback callBack,
 object state
);
[C++]
public: static bool QueueUserWorkItem(
 WaitCallback* callBack,
 Object* state
);
[JScript]
public static function QueueUserWorkItem(
   callBack : WaitCallback,
 state : Object
) : Boolean;

Parameters

callBack
A WaitCallback representing the method to execute.
state
An object containing data to be used by the method.

Return Value

true if the method is successfully queued; otherwise, false.

Remarks

If the queued method requires only a single data item, you can cast that data item to type Object. If the method requires more complex data, you must define a class to contain the data.

[Visual Basic] Note   Visual Basic users can omit the WaitCallback constructor, and simply use the AddressOf operator when passing the callback method to QueueUserWorkItem. Visual Basic automatically calls the correct delegate constructor.

Example

[Visual Basic] 
' 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] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

ThreadPool Class | ThreadPool Members | System.Threading Namespace | ThreadPool.QueueUserWorkItem Overload List | Thread Pooling

Show:
© 2014 Microsoft