Condividi tramite


WaitCallback Delegate

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

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

Syntax

'Declaration
<ComVisibleAttribute(True)> _
Public Delegate Sub WaitCallback ( _
    state As Object _
)
[ComVisibleAttribute(true)]
public delegate void WaitCallback(
    Object state
)

Parameters

  • state
    Type: System.Object
    An object that contains 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 in the Syntax section.

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

NoteNote:

Visual Basic and C# users can omit the WaitCallback constructor, because the compilers infer the delegate type automatically and supply the correct constructor. In Visual Basic, use the AddressOf operator when passing the callback method to QueueUserWorkItem.

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. Every time your callback method executes, the state parameter contains this object.

For more information about how to use callbacks to synchronize thread pool threads, see The Managed Thread Pool.

Examples

The following 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.

The example displays its output in a TextBlock on the UI thread. To access the TextBlock from the callback thread, the example uses the Dispatcher property to obtain a Dispatcher object for the TextBlock, and then uses the Dispatcher.BeginInvoke method to make the cross-thread call.

Imports System.Threading

Class Example

    ' This is the UI element that receives the output from the example.
    Private Shared outputBlock As System.Windows.Controls.TextBlock

    ' The Demo method runs the example. It saves the TextBlock that is 
    ' used for output, and sets up an event handler to start tasks.
    Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

        Example.outputBlock = outputBlock
        outputBlock.Text = "Click here to start a background task." & vbCrLf

        ' Set up an event handler to start a task when the TextBlock 
        ' is clicked.
        AddHandler outputBlock.MouseLeftButtonUp, AddressOf HandleMouseUp

    End Sub

    ' Clicking the TextBlock queues a delegate to perform a task on a 
    ' thread pool thread.
    Private Shared Sub HandleMouseUp(ByVal sender As Object, _
                          ByVal e As System.Windows.Input.MouseButtonEventArgs) 

        ThreadPool.QueueUserWorkItem(AddressOf AppendTextTask)

        ' Note: The stateInfo object for the task in this example is
        ' Nothing, because this overload of QueueUserWorkItem does not
        ' take a stateInfo parameter. 
    End Sub 

    ' This method performs the task, which is to append text to the
    ' TextBlock. To communicate with objects on the UI thread, get the 
    ' Dispatcher for one of the UI objects. Use the Dispatcher object's 
    ' BeginInvoke method to queue a delegate that will run on the UI thread,
    ' and therefore can safely access UI elements like the TextBlock. In 
    ' this example, the delegate is stored in the field named 'append'.
    Private Shared Sub AppendTextTask(ByVal stateInfo As Object)

        outputBlock.Dispatcher.BeginInvoke(append, "Hello from the thread pool." & vbCrLf)

    End Sub

    ' Invoking this delegate runs the AppendText method.
    Private Shared append As New Action(Of String)(AddressOf AppendText) 

    ' The AppendText method can safely access UI elements because it is run
    ' on the UI thread, by using the Dispatcher.BeginInvoke method.
    Private Shared Sub AppendText(ByVal text As String) 

        outputBlock.Text &= text

    End Sub
End Class 

' This code produces output similar to the following:
'
'Click here to start a background task.
'Hello from the thread pool.
'Hello from the thread pool.
'Hello from the thread pool.
using System;
using System.Threading;

class Example
{
    // This is the UI element that receives the output from the example.
    private static System.Windows.Controls.TextBlock outputBlock;

    // The Demo method runs the example. It saves the TextBlock that is 
    // used for output, and sets up an event handler to start tasks.
    public static void Demo(System.Windows.Controls.TextBlock outputBlock)
    {
        Example.outputBlock = outputBlock;
        outputBlock.Text = "Click here to start a background task.\r\n";

        // Set up an event handler to start a task when the TextBlock 
        // is clicked.
        outputBlock.MouseLeftButtonUp += HandleMouseUp;
    }

    // Clicking the TextBlock queues a delegate to perform a task on a 
    // thread pool thread.
    private static void HandleMouseUp(object sender, 
                                      System.Windows.Input.MouseButtonEventArgs e)
    {
        ThreadPool.QueueUserWorkItem(AppendTextTask);

        // Note: The stateInfo object for the task in this example is
        // Nothing, because this overload of QueueUserWorkItem does not
        // take a stateInfo parameter. 
    }

    // This method performs the task, which is to append text to the
    // TextBlock. To communicate with objects on the UI thread, get the 
    // Dispatcher for one of the UI objects. Use the Dispatcher object's 
    // BeginInvoke method to queue a delegate that will run on the UI thread,
    // and therefore can safely access UI elements like the TextBlock.
    private static void AppendTextTask(object stateInfo)
    {
        outputBlock.Dispatcher.BeginInvoke(delegate () {
            outputBlock.Text += "Hello from the thread pool.\r\n";
        });
    }
}

/* This code produces output similar to the following:

Click here to start a background task.
Hello from the thread pool.
Hello from the thread pool.
Hello from the thread pool.
 */

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.