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

Delegado WaitCallback

 

Publicado: octubre de 2016

Representa un método de devolución de llamada que debe ejecutar un subproceso del grupo de subprocesos.

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

<ComVisibleAttribute(True)>
Public Delegate Sub WaitCallback (
	state As Object
)

Parámetros

state
Type: System.Object

Objeto que contiene la información que va a usar el método de devolución de llamada.

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 T:System.Threading.WaitCallback delegate to M:System.Threading.ThreadPool.QueueUserWorkItem(System.Threading.WaitCallback,System.Object). Your callback method executes when a thread pool thread becomes available.

System_CAPS_noteNota

Visual Basic users can omit the T:System.Threading.WaitCallback constructor, and simply use the AddressOf operator when passing the callback method to M:System.Threading.ThreadPool.QueueUserWorkItem(System.Threading.WaitCallback,System.Object). 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 M:System.Threading.ThreadPool.QueueUserWorkItem(System.Threading.WaitCallback,System.Object) 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 [<topic://cpconthreadpooling>].

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 T:System.Threading.WaitCallback delegate to queue a task for execution by the thread pool. The code example uses the M:System.Threading.ThreadPool.QueueUserWorkItem(System.Threading.WaitCallback) method overload to queue a task, which is represented by a T:System.Threading.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.

Imports System.Threading

Public Module Example
    Public Sub Main()
        ' Queue the work for execution.
        ThreadPool.QueueUserWorkItem(AddressOf ThreadProc)

        Console.WriteLine("Main thread does some work, then sleeps.")

        Thread.Sleep(1000)

        Console.WriteLine("Main thread exits.")
    End Sub

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

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.
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
' The example displays output like the following:
'        Main thread does some work, then sleeps.
'        This report displays the number 42.
'        Main thread exits.

.NET Framework
Disponible desde 1.1
Biblioteca de clases portable
Se admite en: plataformas portátiles de .NET
Silverlight
Disponible desde 2.0
Windows Phone Silverlight
Disponible desde 7.0
Volver al principio
Mostrar: