共用方式為


ThreadPool.QueueUserWorkItem Method (WaitCallback, Object)

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

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.

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

Syntax

'Declaration
<SecuritySafeCriticalAttribute> _
Public Shared Function QueueUserWorkItem ( _
    callBack As WaitCallback, _
    state As Object _
) As Boolean
[SecuritySafeCriticalAttribute]
public static bool QueueUserWorkItem(
    WaitCallback callBack,
    Object state
)

Parameters

  • state
    Type: System.Object
    An object that contains data to be used by the method.

Return Value

Type: System.Boolean
true if the method is successfully queued; NotSupportedException is thrown if the work item could not be queued.

Exceptions

Exception Condition
NotSupportedException

The common language runtime (CLR) is hosted, and the host does not support this action.

ArgumentNullException

callBack is nulla null reference (Nothing in Visual Basic).

Remarks

If the callback method requires complex data, you can define a class to contain the data.

NoteNote:

Visual Basic and C# users can omit the WaitCallback delegate constructor when specifying the callback method in calls to the QueueUserWorkItem method. In Visual Basic, use the AddressOf operator. The compilers automatically call the correct delegate constructor.

Examples

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

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. The data passed to the task is an object 
    ' representing the time the task was queued.
    Private Shared Sub HandleMouseUp(ByVal sender As Object, _
                          ByVal e As System.Windows.Input.MouseButtonEventArgs) 

        ThreadPool.QueueUserWorkItem(AddressOf AppendTextTask, DateTime.Now)

    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. Task queued at: " & stateInfo & 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. Task queued at: 6/30/2008 4:38:54 PM
'Hello from the thread pool. Task queued at: 6/30/2008 4:39:04 PM
'Hello from the thread pool. Task queued at: 6/30/2008 4:39:05 PM
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. The data passed to the task is an object 
    // representing the time the task was queued.
    private static void HandleMouseUp(object sender, 
                                      System.Windows.Input.MouseButtonEventArgs e)
    {
        ThreadPool.QueueUserWorkItem(AppendTextTask, DateTime.Now);
    }

    // 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 += String.Format(
                "Hello from the thread pool. Task queued at: {0}\r\n",
                stateInfo);
        });
    }
}

/* This code produces output similar to the following:

Click here to start a background task.
Hello from the thread pool. Task queued at: 6/30/2008 4:41:54 PM
Hello from the thread pool. Task queued at: 6/30/2008 4:42:03 PM
Hello from the thread pool. Task queued at: 6/30/2008 4:42:03 PM
 */

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.