İngilizce dilinde oku

Aracılığıyla paylaş


ThreadStart Temsilci

Tanım

üzerinde Threadyürütülen yöntemi temsil eder.

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

Örnekler

Aşağıdaki kod örneği, bir örnek yöntemiyle ve statik bir ThreadStart yöntemle temsilci oluşturma ve kullanma söz dizimini gösterir.

Temsilci oluşturmayı gösteren başka bir ThreadStart basit örnek için yöntem aşırı yüklemesine Thread.Start() bakın. İş parçacığı oluşturma hakkında daha fazla bilgi için bkz. Başlangıç Zamanında İş Parçacıkları Oluşturma ve Veri Geçirme.

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

Açıklamalar

Yönetilen bir iş parçacığı oluşturulduğunda, iş parçacığında yürütülen yöntem, oluşturucuya Thread geçirilen bir ThreadStart temsilci veya temsilci ParameterizedThreadStart tarafından temsil edilir. yöntemi çağrılana Thread.Start kadar iş parçacığı yürütülmeye başlamaz. Yürütme, veya ParameterizedThreadStart temsilcisi tarafından temsil edilen yöntemin ThreadStart ilk satırında başlar.

Not

Visual Basic ve C# kullanıcıları iş parçacığı oluştururken veya ParameterizedThreadStart temsilci oluşturucuyu ThreadStart atlayabilir. Visual Basic'da, yönteminizi oluşturucuya Thread geçirirken işlecini kullanınAddressOf; örneğin, Dim t As New Thread(AddressOf ThreadProc). C# dilinde iş parçacığı yordamının adını belirtmeniz yeterlidir. Derleyici doğru temsilci oluşturucuyu seçer.

C++ için, .NET Framework 2.0'dan başlayarak statik bir yöntem için temsilci oluşturmak ThreadStart için yalnızca bir parametre gerekir: sınıf adıyla nitelenen geri çağırma yönteminin adresi. Önceki sürümlerde statik bir yöntem için temsilci oluşturulurken iki parametre gerekiyordu: sıfır (null) ve yöntem adresi. Örnek yöntemi için tüm sürümler iki parametre gerektirir: örnek değişkeni ve yöntem adresi.

Uzantı Metotları

GetMethodInfo(Delegate)

Belirtilen temsilci tarafından temsil edilen yöntemi temsil eden bir nesnesi alır.

Şunlara uygulanır

Ürün Sürümler
.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
.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
.NET Standard 2.0, 2.1

Ayrıca bkz.