ParameterizedThreadStart Delegato

Definizione

Rappresenta il metodo eseguito su un oggetto Thread.

public delegate void ParameterizedThreadStart(System::Object ^ obj);
[System.Runtime.InteropServices.ComVisible(false)]
public delegate void ParameterizedThreadStart(object obj);
public delegate void ParameterizedThreadStart(object obj);
[<System.Runtime.InteropServices.ComVisible(false)>]
type ParameterizedThreadStart = delegate of obj -> unit
type ParameterizedThreadStart = delegate of obj -> unit
Public Delegate Sub ParameterizedThreadStart(obj As Object)

Parametri

obj
Object

Oggetto che contiene dati per la routine thread.

Attributi

Esempio

Nell'esempio di codice seguente viene usato un ParameterizedThreadStart delegato per eseguire un metodo statico e un metodo di istanza. Il primo ParameterizedThreadStart delegato è rappresentato dal metodo statico DoWork e il secondo è rappresentato dal metodo di istanza DoMoreWork . Entrambi i metodi corrispondono alla firma del ParameterizedThreadStart delegato, ovvero hanno un singolo parametro di tipo Object e non restituiscono un valore.

Note

I compilatori Visual Basic e C# deducono il delegato ParameterizedThreadStart dalle firme dei metodi DoWork e DoMoreWork e chiamare il costruttore corretto. Pertanto, nel codice non è presente alcuna chiamata esplicita al costruttore.

using System;
using System.Threading;

public class Work
{
    public static void Main()
    {
        // Start a thread that calls a parameterized static method.
        Thread newThread = new Thread(Work.DoWork);
        newThread.Start(42);

        // Start a thread that calls a parameterized instance method.
        Work w = new Work();
        newThread = new Thread(w.DoMoreWork);
        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 example displays output like the following:
//       Static thread procedure. Data='42'
//       Instance thread procedure. Data='The answer.'
Imports System.Threading

Public Class Work
    Shared Sub Main()
        ' Start a thread that calls a parameterized static method.
        Dim newThread As New Thread(AddressOf Work.DoWork)
        newThread.Start(42)

        ' Start a thread that calls a parameterized instance method.
        Dim w As New Work()
        newThread = New Thread(AddressOf w.DoMoreWork)
        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 example displays output like the following:
'    Static thread procedure. Data='42'
'    Instance thread procedure. Data='The answer.'

Commenti

Quando viene creato un thread gestito, il metodo eseguito nel thread è rappresentato da:

Il thread non inizia l'esecuzione finché non viene chiamato il Thread.Start metodo . Il ThreadStart delegato o ParameterizedThreadStart viene richiamato sul thread e l'esecuzione inizia alla prima riga del metodo rappresentato dal delegato. Nel caso del ParameterizedThreadStart delegato, l'oggetto passato al Start(Object) metodo viene passato al delegato.

Note

Visual Basic e C# gli utenti possono omettere il costruttore delegato ThreadStart o ParameterizedThreadStart durante la creazione di un thread. In Visual Basic usare l'operatore AddressOf quando si passa il metodo al costruttore Thread, ad esempio Dim t As New Thread(AddressOf ThreadProc). In C#, specificare semplicemente il nome della routine del thread. Il compilatore seleziona il costruttore delegato corretto.

Note

Quando si crea un delegato per un ParameterizedThreadStart metodo di istanza in C++, il primo parametro del costruttore è la variabile di istanza. Per un metodo statico, il primo parametro del costruttore è zero. Per un metodo statico, il costruttore delegato richiede un solo parametro: l'indirizzo del metodo di callback, qualificato dal nome della classe.

Il ParameterizedThreadStart delegato e l'overload del metodo semplificano il Thread.Start(Object) passaggio di dati a una routine thread, ma questa tecnica non è indipendente dai tipi perché qualsiasi oggetto può essere passato a Thread.Start(Object). Un modo più affidabile per passare dati a una routine thread consiste nell'inserire sia la routine thread che i campi dati in un oggetto di lavoro. Per altre informazioni, vedere Creazione di thread e passaggio di dati all'ora di inizio.

Il ParameterizedThreadStart delegato supporta solo un singolo parametro. È possibile passare più elementi di dati a ParameterizedThreadStart facendo in modo che tale parametro sia uno dei seguenti:

Metodi di estensione

Nome Descrizione
GetMethodInfo(Delegate)

Ottiene un oggetto che rappresenta il metodo rappresentato dal delegato specificato.

Si applica a

Vedi anche