Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

WaitCallback Delegate

Represents a callback method to be executed by a thread pool thread.

[Visual Basic]
<Serializable>
Public Delegate Sub WaitCallback( _
   ByVal state As Object _
)
[C#]
[Serializable]
public delegate void WaitCallback(
   object state
);
[C++]
[Serializable]
public __gc __delegate void WaitCallback(
   Object* state
);

[JScript] In JScript, you can use the delegates in the .NET Framework, but you cannot define your own.

Parameters [Visual Basic, C#, C++]

The declaration of your callback method must have the same parameters as the WaitCallback delegate declaration.

state
An object containing information to be used by the callback method.

Remarks

WaitCallback represents a callback method that you want to execute on a ThreadPool thread. Create the delegate by passing your callback method to the WaitCallback constructor. Your method must have the signature shown here.

Queue your task for execution by passing the WaitCallback delegate to ThreadPool.QueueUserWorkItem. Your callback method executes when a thread pool thread becomes available.

[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.

If you want to pass information to your callback method, create an object that contains the necessary information and pass it to QueueUserWorkItem when you queue your task for execution. Each time your callback method executes, the state parameter contains this object.

For more information about using callbacks to synchronize thread pool threads, see Thread Pooling.

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

Namespace: System.Threading

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

Assembly: Mscorlib (in Mscorlib.dll)

See Also

System.Threading Namespace | ThreadPool | Threads and Threading | Thread Pooling

Show:
© 2014 Microsoft