Export (0) Print
Expand All
0 out of 1 rated this helpful - Rate this topic

ThreadPool.QueueUserWorkItem Method (WaitCallback, Object)

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)
'Declaration
Public Shared Function QueueUserWorkItem ( _
	callBack As WaitCallback, _
	state As Object _
) As Boolean

Parameters

callBack
Type: System.Threading.WaitCallback

A WaitCallback representing the method to execute.

state
Type: System.Object

An object containing 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.
ExceptionCondition
NotSupportedException

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

ArgumentNullException

callBack is Nothing.

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

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.

Version Information

In the .NET Framework version 2.0, the Thread.CurrentPrincipal property value is propagated to worker threads queued using the QueueUserWorkItem method. In earlier versions, the principal information is not propagated.

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.

' 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. 
Imports System
Imports 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 Boilerplate As String 
    Public Value As Integer 

    ' Public constructor provides an easy way to supply all 
    ' the information needed for the task. 
    Public Sub New(text As String, number As Integer)
        Boilerplate = text
        Value = number
    End Sub 
End Class 

Public Class Example

    <MTAThread> _
    Public Shared Sub Main()
        ' Create an object containing the information needed 
        ' for the task. 
        Dim ti As New TaskInfo("This report displays the number {0}.", 42)

        ' Queue the task and data.
        ThreadPool.QueueUserWorkItem( _
                New WaitCallback(AddressOf 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.")
    End Sub 

    ' The thread procedure performs the independent task, in this case 
    ' formatting and printing a very simple report. 
    
    Shared Sub ThreadProc(stateInfo As Object)
        Dim ti As TaskInfo = CType(stateInfo, TaskInfo)
        Console.WriteLine(ti.Boilerplate, ti.Value)
    End Sub 
End Class

.NET Framework

Supported in: 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.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.