ThreadStart Temsilci
Tanım
Önemli
Bazı bilgiler ürünün ön sürümüyle ilgilidir ve sürüm öncesinde önemli değişiklikler yapılmış olabilir. Burada verilen bilgilerle ilgili olarak Microsoft açık veya zımni hiçbir garanti vermez.
üzerinde Threadyürütülen yöntemi temsil eder.
public delegate void ThreadStart();
public delegate void ThreadStart();
[System.Runtime.InteropServices.ComVisible(true)]
public delegate void ThreadStart();
type ThreadStart = delegate of unit -> unit
[<System.Runtime.InteropServices.ComVisible(true)>]
type ThreadStart = delegate of unit -> unit
Public Delegate Sub ThreadStart()
- Öznitelikler
Örnekler
Aşağıdaki kod örneği, ö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ığı Oluşturma ve Veri Geçirme.
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
Açıklamalar
Yönetilen 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öntem ç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.
Note
Visual Basic ve C# kullanıcıları, iş parçacığı oluştururken ThreadStart veya ParameterizedThreadStart temsilci oluşturucuyu atlayabilir. Visual Basic'da, yönteminizi Thread oluşturucuya geçirirken AddressOf işlecini kullanın; ö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.
Uzantı Metotları
| Name | Description |
|---|---|
| GetMethodInfo(Delegate) |
Belirtilen temsilci tarafından temsil edilen yöntemi temsil eden bir nesnesi alır. |