ThreadStart 代理人
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
代表在 上 Thread執行的方法。
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()
- 屬性
範例
以下程式碼範例展示了建立並使用 ThreadStart 代理,搭配實例方法與靜態方法的語法。
另一個簡單示範如何建立 ThreadStart 代理的例子,請參見 Thread.Start() 方法過載。 欲了解更多關於執行緒建立的資訊,請參閱「 建立執行緒及開始時傳遞資料」。
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
備註
當建立受管理執行緒時,執行緒上執行的方法會由ThreadStartParameterizedThreadStart一個代理或一個代理代表來表示,並傳遞給建構子。Thread 執行緒直到 Thread.Start 方法被呼叫前才開始執行。 執行從 or ParameterizedThreadStart 代表的方法ThreadStart的第一行開始。
Note
Visual Basic 和 C# 使用者在建立執行緒時可以省略 ThreadStart 或 ParameterizedThreadStart 的代理建構子。 在Visual Basic中,將方法傳給 Thread 建構子時,使用 AddressOf 運算子;例如,Dim t As New Thread(AddressOf ThreadProc)。 在 C# 中,只需指定執行緒程序的名稱即可。 編譯器會選擇正確的代理建構器。
擴充方法
| 名稱 | Description |
|---|---|
| GetMethodInfo(Delegate) |
取得一個代表指定代理所代表方法的物件。 |