Este artículo se tradujo automáticamente. Para ver el artículo en inglés, active la casilla Inglés. Además, puede mostrar el texto en inglés en una ventana emergente si mueve el puntero del mouse sobre el texto.
Traducción
Inglés

ThreadPool.QueueUserWorkItem (Método) (WaitCallback)

 

Pone en cola un método para su ejecución.El método se ejecuta cuando hay disponible un subproceso de grupo de subprocesos.

Espacio de nombres:   System.Threading
Ensamblado:  mscorlib (en mscorlib.dll)

Public Shared Function QueueUserWorkItem (
	callBack As WaitCallback
) As Boolean

Parámetros

callBack

WaitCallback que representa el método que se va a ejecutar.

Valor devuelto

Type: System.Boolean

true si el método se pone en la cola correctamente; se produce la excepción NotSupportedException si el elemento de trabajo no se puede poner en la cola.

Exception Condition
ArgumentNullException

callBack is null.

NotSupportedException

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

You can place data required by the queued method in the instance fields of the class in which the method is defined, or you can use the QueueUserWorkItem(WaitCallback, Object) overload that accepts an object containing the necessary data.

System_CAPS_noteNota

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.

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 uses the QueueUserWorkItem(WaitCallback) method overload to queue a task, which is represented by 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.

Imports System
Imports System.Threading

Public Class Example

    <MTAThread> _
    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
' The example displays output like the following:
'       Main thread does some work, then sleeps.
'       Hello from the thread pool.
'       Main thread exits.

.NET Framework
Disponible desde 1.1
Portable Class Library
Compatible con: portable .NET platforms
Silverlight
Disponible desde 2.0
Windows Phone Silverlight
Disponible desde 7.0
Volver al principio
Mostrar: