Export (0) Print
Expand All
Expand Minimize

WaitCallback Delegate

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

Namespace:  System.Threading
Assembly:  mscorlib (in mscorlib.dll)

[ComVisibleAttribute(true)]
public delegate void WaitCallback(
	Object state
)

Parameters

state
Type: System.Object

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

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]

NoteNote

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 The Managed Thread Pool.

This section contains two examples. The first example demonstrates how you can queue a simple task for execution by the thread pool.

The second example demonstrates how you can queue a task and pass information to it.

Example 1

This example shows how to use the WaitCallback delegate to queue a task for execution by the thread pool. The code example uses the ThreadPool.QueueUserWorkItem(WaitCallback) method overload to queue a task, which is represented by a WaitCallback that wraps the ThreadProc method, to execute when a thread becomes available. No task information is supplied with this overload. Therefore, the information that is available to the ThreadProc method is limited to the object the method belongs to.

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

Example 2

This example shows how to create an object that contains task information. It also demonstrates how to pass that object to a task that is queued for execution by the thread pool.

// 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.
        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.");
    }

    // 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); 
    }
}

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft