ThreadStart Delegat

Definicja

Reprezentuje metodę wykonywaną w obiekcie Thread.

C#
public delegate void ThreadStart();
C#
[System.Runtime.InteropServices.ComVisible(true)]
public delegate void ThreadStart();
Atrybuty

Przykłady

Poniższy przykład kodu przedstawia składnię tworzenia i używania delegata ThreadStart z metodą wystąpienia oraz metodą statyczną.

Aby zapoznać się z innym prostym przykładem, który pokazuje, jak utworzyć delegata ThreadStart , zobacz Thread.Start() przeciążenie metody. Aby uzyskać więcej informacji na temat tworzenia wątków, zobacz Creating Threads and Passing Data at Start Time (Tworzenie wątków i przekazywanie danych w czasie rozpoczęcia).

C#
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
 */

Uwagi

Po utworzeniu zarządzanego wątku metoda wykonywana w wątku jest reprezentowana przez ThreadStart delegata lub ParameterizedThreadStart delegata przekazanego do konstruktora Thread . Wątek nie rozpoczyna wykonywania, dopóki Thread.Start metoda nie zostanie wywołana. Wykonanie rozpoczyna się w pierwszym wierszu metody reprezentowanej przez delegata ThreadStart lub ParameterizedThreadStart .

Uwaga

Użytkownicy języka Visual Basic i C# mogą pominąć ThreadStart konstruktora lub ParameterizedThreadStart delegata podczas tworzenia wątku. W języku Visual Basic użyj AddressOf operatora podczas przekazywania metody do konstruktora Thread , Dim t As New Thread(AddressOf ThreadProc)na przykład . W języku C# po prostu określ nazwę procedury wątku. Kompilator wybiera prawidłowy konstruktor delegata.

W przypadku języka C++, począwszy od .NET Framework 2.0, utworzenie ThreadStart delegata dla metody statycznej wymaga tylko jednego parametru: adresu metody wywołania zwrotnego, kwalifikowanej przez nazwę klasy. We wcześniejszych wersjach dwa parametry były wymagane podczas tworzenia delegata dla metody statycznej: zero (null) i adres metody. W przypadku metody wystąpienia wszystkie wersje wymagają dwóch parametrów: zmiennej wystąpienia i adresu metody.

Metody rozszerzania

GetMethodInfo(Delegate)

Pobiera obiekt reprezentujący metodę reprezentowaną przez określonego delegata.

Dotyczy

Produkt Wersje
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Zobacz też