ThreadStart ThreadStart ThreadStart ThreadStart Delegate

Definizione

Rappresenta il metodo eseguito in un oggetto Thread.Represents the method that executes on a Thread.

public delegate void ThreadStart();
[System.Runtime.InteropServices.ComVisible(true)]
public delegate void ThreadStart();
type ThreadStart = delegate of unit -> unit
Public Delegate Sub ThreadStart()
Ereditarietà
ThreadStartThreadStartThreadStartThreadStart
Attributi

Esempi

Nell'esempio di codice seguente viene illustrata la sintassi per la ThreadStart creazione e l'utilizzo di un delegato con un metodo di istanza e con un metodo statico.The following code example shows the syntax for creating and using a ThreadStart delegate with an instance method and with a static method.

Per un altro esempio semplice in cui viene illustrato come ThreadStart creare un delegato, Thread.Start() vedere l'overload del metodo.For another simple example that demonstrates how to create a ThreadStart delegate, see the Thread.Start() method overload. Per altre informazioni sulla creazione di thread, vedere creazione di thread e passaggio di dati all'ora di inizio.For more information about thread creation, see Creating Threads and Passing Data at Start Time.

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
 */
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 explicitly. 
        // 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
 */
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

Commenti

Quando viene creato un thread gestito, il metodo che viene eseguito sul thread è rappresentato da un ThreadStart delegato o da un ParameterizedThreadStart delegato passato al Thread costruttore.When a managed thread is created, the method that executes on the thread is represented by a ThreadStart delegate or a ParameterizedThreadStart delegate that is passed to the Thread constructor. Il thread non inizia l'esecuzione fino a quando Thread.Start non viene chiamato il metodo.The thread does not begin executing until the Thread.Start method is called. L'esecuzione inizia in corrispondenza della prima riga del metodo rappresentato dal ThreadStart delegato ParameterizedThreadStart o.Execution begins at the first line of the method represented by the ThreadStart or ParameterizedThreadStart delegate.

Nota

Visual Basic e C# gli utenti possono omettere ParameterizedThreadStart il ThreadStart costruttore di delegati o quando si crea un thread.Visual Basic and C# users can omit the ThreadStart or ParameterizedThreadStart delegate constructor when creating a thread. In Visual Basic usare l' AddressOf operatore quando Thread si passa il metodo al Dim t As New Thread(AddressOf ThreadProc)costruttore, ad esempio.In Visual Basic, use the AddressOf operator when passing your method to the Thread constructor; for example, Dim t As New Thread(AddressOf ThreadProc). In C#è sufficiente specificare il nome della routine del thread.In C#, simply specify the name of the thread procedure. Il compilatore seleziona il costruttore di delegato corretto.The compiler selects the correct delegate constructor.

Per C++, a partire da .NET Framework 2,0, la ThreadStart creazione di un delegato per un metodo statico richiede un solo parametro, ovvero l'indirizzo del metodo di callback, qualificato dal nome della classe.For C++, starting with .NET Framework 2.0, creating a ThreadStart delegate for a static method requires only one parameter: the address of the callback method, qualified by the class name. Nelle versioni precedenti sono necessari due parametri quando si crea un delegato per un metodo statico: zero (null) e l'indirizzo del metodo.In earlier versions two parameters were required when creating a delegate for a static method: zero (null) and the method address. Per un metodo di istanza, tutte le versioni richiedono due parametri: la variabile di istanza e l'indirizzo del metodo.For an instance method, all versions require two parameters: the instance variable and the method address.

Metodi di estensione

GetMethodInfo(Delegate) GetMethodInfo(Delegate) GetMethodInfo(Delegate) GetMethodInfo(Delegate)

Ottiene un oggetto che rappresenta il metodo rappresentato dal delegato specificato.Gets an object that represents the method represented by the specified delegate.

Si applica a

Vedi anche