Versión imprimible       Enviar     
Evaluar y enviar comentarios
MSDN
MSDN Library
 ParameterizedThreadStart (Delegado)
Contraer todo/Expandir todo Contraer todo
Esta página es específica de
Microsoft Visual Studio 2005/.NET Framework 2.0

Hay además otras versiones disponibles para:
ParameterizedThreadStart (Delegado)
Representa el método que se ejecuta en una clase Thread.

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

Visual Basic (Declaración)
<ComVisibleAttribute(False)> _
Public Delegate Sub ParameterizedThreadStart ( _
    obj As Object _
)
Visual Basic (Uso)
Dim instance As New ParameterizedThreadStart(AddressOf HandlerMethod)
C#
[ComVisibleAttribute(false)] 
public delegate void ParameterizedThreadStart (
    Object obj
)
C++
[ComVisibleAttribute(false)] 
public delegate void ParameterizedThreadStart (
    Object^ obj
)
J#
/** @delegate */
/** @attribute ComVisibleAttribute(false) */ 
public delegate void ParameterizedThreadStart (
    Object obj
)
JScript

    
XAML
No aplicable.

Parámetros

obj

Objeto que contiene los datos para el procedimiento de subproceso.

Cuando se crea un subproceso administrado, un delegado ThreadStart o ParameterizedThreadStart que se pasa al constructor Thread representa al método que se ejecuta en el subproceso. El subproceso no comienza a ejecutarse hasta que se llama al método System.Threading.Thread.Start. Se invoca al delegado ThreadStart o ParameterizedThreadStart en el subproceso y comienza la ejecución en la primera línea del método representado por el delegado. En el caso del delegado ParameterizedThreadStart, el objeto que se pasa al método Start(Object) se pasa al delegado.

NotaNota:

Los usuarios de Visual Basic y C# pueden omitir el constructor del delegado ThreadStart o ParameterizedThreadStart al crear un subproceso. En Visual Basic, al pasar el método al constructor Thread, se debe utilizar el operador AddressOf; por ejemplo, Dim t As New Thread(AddressOf ThreadProc). En C#, basta con especificar el nombre del procedimiento de subproceso. El compilador selecciona el constructor del delegado correcto.

NotaNota:

Cuando se crea un delegado ParameterizedThreadStart para el método de una instancia en C++, el primer parámetro del constructor es la variable de la instancia. Para un método estático, el primer parámetro del constructor es cero. Para un método estático, el constructor del delegado sólo requiere un parámetro: la dirección del método de devolución de llamada, cualificada por el nombre de clase.

El delegado ParameterizedThreadStart y la sobrecarga del método Thread.Start(Object) facilitan el paso de los datos a un procedimiento de subproceso, pero esta técnica carece de seguridad de tipos puesto que se puede pasar cualquier objeto a Thread.Start(Object). Una forma más segura de pasar los datos a un procedimiento de subproceso es colocarlo junto con los campos de datos en un objeto de trabajo. Para obtener más información, vea Crear subprocesos y analizar los datos en el inicio.

En el ejemplo de código siguiente se muestra la sintaxis para crear y utilizar un delegado ParameterizedThreadStart con un método estático y un método de instancia.

Visual Basic
Imports System
Imports System.Threading

Public Class Work

    <MTAThread> _
    Shared Sub Main()
        ' To start a thread using a shared thread procedure, use
        ' the class name and method name when you create the 
        ' ParameterizedThreadStart delegate. Visual Basic expands
        ' the AddressOf expression to the appropriate delegate 
        ' creation syntax:
        '    New ParameterizedThreadStart(AddressOf Work.DoWork)
        '
        Dim newThread As New Thread(AddressOf Work.DoWork)
        
        ' Use the overload of the Start method that has a
        ' parameter of type Object. You can create an object that
        ' contains several pieces of data, or you can pass any 
        ' object or value type. The following code passes the
        ' integer value 42.
        '
        newThread.Start(42)

        ' To start a thread using an instance method for the thread 
        ' procedure, use the instance variable and method name when 
        ' you create the ParameterizedThreadStart delegate. Visual 
        ' Basic expands the AddressOf expression to the appropriate 
        ' delegate creation syntax:
        '    New ParameterizedThreadStart(AddressOf w.DoMoreWork)
        '
        Dim w As New Work()
        newThread = New Thread(AddressOf w.DoMoreWork)
        
        ' Pass an object containing data for the thread.
        '
        newThread.Start("The answer.")
    End Sub
 
    Public Shared Sub DoWork(ByVal data As Object)
        Console.WriteLine("Static thread procedure. Data='{0}'", _
            data)
    End Sub

    Public Sub DoMoreWork(ByVal data As Object) 
        Console.WriteLine("Instance thread procedure. Data='{0}'", _
            data)
    End Sub
End Class

' This code example produces the following output (the order 
'   of the lines might vary):
'
'Static thread procedure. Data='42'
'Instance thread procedure. Data='The answer'
C#
using System;
using System.Threading;

public class Work
{
    public static void Main()
    {
        // To start a thread using a shared thread procedure, use
        // the class name and method name when you create the 
        // ParameterizedThreadStart delegate. C# infers the 
        // appropriate delegate creation syntax:
        //    new ParameterizedThreadStart(Work.DoWork)
        //
        Thread newThread = new Thread(Work.DoWork);
        
        // Use the overload of the Start method that has a
        // parameter of type Object. You can create an object that
        // contains several pieces of data, or you can pass any 
        // reference type or value type. The following code passes
        // the integer value 42.
        //
        newThread.Start(42);

        // To start a thread using an instance method for the thread 
        // procedure, use the instance variable and method name when 
        // you create the ParameterizedThreadStart delegate. C# infers 
        // the appropriate delegate creation syntax:
        //    new ParameterizedThreadStart(w.DoMoreWork)
        //
        Work w = new Work();
        newThread = new Thread(w.DoMoreWork);
        
        // Pass an object containing data for the thread.
        //
        newThread.Start("The answer.");
    }
 
    public static void DoWork(object data)
    {
        Console.WriteLine("Static thread procedure. Data='{0}'",
            data);
    }

    public void DoMoreWork(object data)
    {
        Console.WriteLine("Instance thread procedure. Data='{0}'",
            data);
    }
}

/* This code example produces the following output (the order 
   of the lines might vary):

Static thread procedure. Data='42'
Instance thread procedure. Data='The answer'
*/
C++
using namespace System;
using namespace System::Threading;

namespace SystemThreadingExample
{
    public ref class Work
    {
    public:
        void StartThreads()
        {
              
            // To start a thread using a shared thread procedure, use
            // the class name and method name when you create the 
            // ParameterizedThreadStart delegate.
            //    AddressOf Work.DoWork)
            //
            Thread^ newThread = gcnew 
                Thread(gcnew ParameterizedThreadStart(Work::DoWork));
              
            // Use the overload of the Start method that has a
            // parameter of type Object. You can create an object that
            // contains several pieces of data, or you can pass any 
            // reference type or value type. The following code passes
            // the integer value 42.
            newThread->Start(42);
              
            // To start a thread using an instance method for the thread 
            // procedure, use the instance variable and method name when 
            // you create the ParameterizedThreadStart delegate.
            Work^ someWork = gcnew Work;
            newThread = 
                gcnew Thread(
                gcnew ParameterizedThreadStart(someWork, 
                &Work::DoMoreWork));
              
            // Pass an object containing data for the thread.
            //
            newThread->Start("The answer.");
        }

        static void DoWork(Object^ data)
        {
            Console::WriteLine("Static thread procedure. Data='{0}'", 
                data);
        }

        void DoMoreWork(Object^ data)
        {
            Console::WriteLine("Instance thread procedure. Data='{0}'", 
                data);
        }
    };
}

//Entry point of example application
int main()
{
    SystemThreadingExample::Work^ samplework = 
        gcnew SystemThreadingExample::Work();
    samplework->StartThreads();
}

// This code example produces the following output (the order 
//   of the lines might vary):

// Static thread procedure. Data='42'
// Instance thread procedure. Data='The answer'

Windows 98, Windows 2000 Service Pack 4, Windows Millennium, Windows Server 2003, Windows XP Media Center, Windows XP Professional x64, Windows XP SP2, Windows XP Starter

Microsoft .NET Framework 3.0 es compatible con Windows Vista, Microsoft Windows XP SP2 y Windows Server 2003 SP1.

.NET Framework

Compatible con: 3.0, 2.0
© 2009 Microsoft Corporation. Reservados todos los derechos. Términos de uso | Marcas Registradas | Privacidad
Page view tracker