Export (0) Print
Expand All

ThreadPool.QueueUserWorkItem Method (WaitCallback)

.NET Framework 1.1

Queues a method for execution. The method executes when a thread pool thread becomes available.

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

Parameters

callBack
A WaitCallback representing the method to be executed.

Return Value

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

Remarks

You can place data required by the queued method in the instance fields of the class in which the method is defined, or you can use the QueueUserWorkItem overload that accepts an object containing the necessary 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] 
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] 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