Representa el método que se ejecuta en un objeto
Thread.
Espacio de nombres: System.Threading
Ensamblado: mscorlib (en mscorlib.dll)
Visual Basic (Declaración)
<ComVisibleAttribute(True)> _
Public Delegate Sub ThreadStart ()
Dim instance As New ThreadStart(AddressOf HandlerMethod)
[ComVisibleAttribute(true)]
public delegate void ThreadStart ()
[ComVisibleAttribute(true)]
public delegate void ThreadStart ()
/** @delegate */
/** @attribute ComVisibleAttribute(true) */
public delegate void ThreadStart ()
Cuando se crea un subproceso administrado, el método que se ejecuta en el subproceso queda representado por un delegado ThreadStart o ParameterizedThreadStart que se haya pasado al constructor Thread. Pero el subproceso no comenzará a ejecutarse hasta que se llame al método System.Threading.Thread.Start. La ejecución comenzará en la primera línea del método representado por el delegado ThreadStart o ParameterizedThreadStart.
Nota: |
|---|
| 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 de delegados correcto. |
Nota: |
|---|
| En la versión 2.0 de .NET Framework, cuando se crea un delegado ThreadStart para un método estático en C++, sólo se requiere un parámetro: la dirección del método de devolución de la llamada, calificada por el nombre de clase. En versiones anteriores, para crear un delegado para un método estático se requerían dos parámetros: cero (null) y la dirección de método. En el caso de un método de instancia, todas las versiones requieren dos parámetros: la variable de instancia y la dirección del método. |
En el siguiente ejemplo de código se muestra la sintaxis para crear y utilizar un delegado ThreadStart con un método de instancia y un método estático.
Para obtener otro ejemplo simple sobre la forma crear un delegado ThreadStart, vea la sobrecarga del método Thread.Start. Para obtener más información sobre la creación de subprocesos, vea Crear subprocesos y analizar los datos en el inicio.
Imports System
Imports System.Threading
Public Class Test
<MTAThread> _
Shared Sub Main()
' To start a thread using a static thread procedure, use the
' class name and method name when you create the ThreadStart
' delegate. Visual Basic expands the AddressOf expression
' to the appropriate delegate creation syntax:
' New ThreadStart(AddressOf Work.DoWork)
'
Dim newThread As New Thread(AddressOf Work.DoWork)
newThread.Start()
' To start a thread using an instance method for the thread
' procedure, use the instance variable and method name when
' you create the ThreadStart delegate. Visual Basic expands
' the AddressOf expression to the appropriate delegate
' creation syntax:
' New ThreadStart(AddressOf w.DoMoreWork)
'
Dim w As New Work()
w.Data = 42
newThread = new Thread(AddressOf w.DoMoreWork)
newThread.Start()
End Sub
End Class
Public Class Work
Public Shared Sub DoWork()
Console.WriteLine("Static thread procedure.")
End Sub
Public Data As Integer
Public Sub DoMoreWork()
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.
'Instance thread procedure. Data=42
using System;
using System.Threading;
class Test
{
static void Main()
{
// To start a thread using a static thread procedure, use the
// class name and method name when you create the ThreadStart
// delegate. Beginning in version 2.0 of the .NET Framework,
// it is not necessary to create a delegate explicityly.
// Specify the name of the method in the Thread constructor,
// and the compiler selects the correct delegate. For example:
//
// Thread newThread = new Thread(Work.DoWork);
//
ThreadStart threadDelegate = new ThreadStart(Work.DoWork);
Thread newThread = new Thread(threadDelegate);
newThread.Start();
// To start a thread using an instance method for the thread
// procedure, use the instance variable and method name when
// you create the ThreadStart delegate. Beginning in version
// 2.0 of the .NET Framework, the explicit delegate is not
// required.
//
Work w = new Work();
w.Data = 42;
threadDelegate = new ThreadStart(w.DoMoreWork);
newThread = new Thread(threadDelegate);
newThread.Start();
}
}
class Work
{
public static void DoWork()
{
Console.WriteLine("Static thread procedure.");
}
public int Data;
public void DoMoreWork()
{
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.
Instance thread procedure. Data=42
*/
using namespace System;
using namespace System::Threading;
ref class Work
{
public:
static void DoWork()
{
Console::WriteLine( "Static thread procedure." );
}
int Data;
void DoMoreWork()
{
Console::WriteLine( "Instance thread procedure. Data={0}", Data );
}
};
int main()
{
// To start a thread using an instance method for the thread
// procedure, specify the object as the first argument of the
// ThreadStart constructor.
//
Work^ w = gcnew Work;
w->Data = 42;
ThreadStart^ threadDelegate = gcnew ThreadStart( w, &Work::DoMoreWork );
Thread^ newThread = gcnew Thread( threadDelegate );
newThread->Start();
// To start a thread using a static thread procedure, specify
// only the address of the procedure. This is a change from
// earlier versions of the .NET Framework, which required
// two arguments, the first of which was null (0).
//
threadDelegate = gcnew ThreadStart( &Work::DoWork );
newThread = gcnew Thread( threadDelegate );
newThread->Start();
}
/* This code example produces the following output (the order
of the lines might vary):
Static thread procedure.
Instance thread procedure. Data=42
*/
import System.*;
import System.Threading.*;
import System.Threading.Thread;
class Test
{
public static void main(String[] args)
{
ThreadStart threadDelegate = new ThreadStart(Work.DoWork);
Thread newThread = new Thread(threadDelegate);
newThread.Start();
} //main
} //Test
class Work
{
public static void DoWork()
{
} //DoWork
} //Work
Windows 98, Windows 2000 Service Pack 4, Windows CE, Windows Millennium, Windows Mobile para Pocket PC, Windows Mobile para Smartphone, 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, 1.1, 1.0
.NET Compact Framework
Compatible con: 2.0, 1.0
XNA Framework
Compatible con: 1.0