Thread 建構函式

定義

初始化 Thread 類別的新執行個體。

多載

Thread(ParameterizedThreadStart)

初始化 Thread 類別的新執行個體,並指定委派,讓物件可以在執行緒啟動時傳遞到執行緒。

Thread(ThreadStart)

初始化 Thread 類別的新執行個體。

Thread(ParameterizedThreadStart, Int32)

初始化 Thread 類別的新執行個體,指定委派,讓物件可以在執行緒啟動時傳遞到執行緒,並指定執行緒的堆疊大小上限。

Thread(ThreadStart, Int32)

初始化 Thread 類別的新執行個體,並指定執行緒的堆疊大小上限。

Thread(ParameterizedThreadStart)

來源:
Thread.cs
來源:
Thread.cs
來源:
Thread.cs

初始化 Thread 類別的新執行個體,並指定委派,讓物件可以在執行緒啟動時傳遞到執行緒。

public:
 Thread(System::Threading::ParameterizedThreadStart ^ start);
public Thread (System.Threading.ParameterizedThreadStart start);
new System.Threading.Thread : System.Threading.ParameterizedThreadStart -> System.Threading.Thread
Public Sub New (start As ParameterizedThreadStart)

參數

start
ParameterizedThreadStart

委派,代表在這個執行緒開始執行時要叫用的方法。

例外狀況

startnull

範例

下列範例示範搭配靜態方法和實例方法建立和使用 ParameterizedThreadStart 委派的語法。

using namespace System;
using namespace System::Threading;

namespace SystemThreadingExample
{
    public ref class Work
    {
    public:
        void StartThreads()
        {
            // Start a thread that calls a parameterized static method.
            Thread^ newThread = gcnew
                Thread(gcnew ParameterizedThreadStart(Work::DoWork));
            newThread->Start(42);
              
            // Start a thread that calls a parameterized instance method.
            Work^ someWork = gcnew Work;
            newThread = gcnew Thread(
                        gcnew ParameterizedThreadStart(someWork,
                        &Work::DoMoreWork));
            newThread->Start("The answer.");
        }

        static void DoWork(Object^ data)
        {
            Console::WriteLine("Static thread procedure. Data='{0}'", 
                data);
        }

        void DoMoreWork(Object^ data)
        {
            Console::WriteLine("Instance thread procedure. Data='{0}'", 
                data);
        }
    };
}

//Entry point of example application
int main()
{
    SystemThreadingExample::Work^ samplework = 
        gcnew SystemThreadingExample::Work();
    samplework->StartThreads();
}
// This example displays output like the following:
//       Static thread procedure. Data='42'
//       Instance thread procedure. Data='The answer.'
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.'
open System.Threading

type Work() =
    static member DoWork(data: obj) =
        printfn $"Static thread procedure. Data='{data}'"

    member _.DoMoreWork(data: obj) =
        printfn $"Instance thread procedure. Data='{data}'"

// Start a thread that calls a parameterized static method.
let newThread = Thread(ParameterizedThreadStart Work.DoWork)
newThread.Start 42

// Start a thread that calls a parameterized instance method.
let w = Work()
let newThread2 = Thread(ParameterizedThreadStart w.DoMoreWork)
newThread.Start "The answer."

// 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.'

備註

執行緒在建立時不會開始執行。 若要排程執行緒執行,請呼叫 Start 方法。 若要將資料物件傳遞至執行緒,請使用 Start(Object) 方法多載。

注意

Visual Basic 使用者可以在建立執行緒時省略 ThreadStart 建構函式。 傳遞方法時, AddressOf 請使用 運算子,例如 Dim t As New Thread(AddressOf ThreadProc) 。 Visual Basic 會自動呼叫建 ThreadStart 構函式。

另請參閱

適用於

Thread(ThreadStart)

來源:
Thread.cs
來源:
Thread.cs
來源:
Thread.cs

初始化 Thread 類別的新執行個體。

public:
 Thread(System::Threading::ThreadStart ^ start);
public Thread (System.Threading.ThreadStart start);
new System.Threading.Thread : System.Threading.ThreadStart -> System.Threading.Thread
Public Sub New (start As ThreadStart)

參數

start
ThreadStart

ThreadStart 委派,代表在這個執行緒開始執行時要叫用的方法。

例外狀況

start 參數為 null

範例

下列程式碼範例示範如何建立執行靜態方法的執行緒。

using namespace System;
using namespace System::Threading;
ref class Work
{
private:
   Work(){}


public:
   static void DoWork(){}

};

int main()
{
   Thread^ newThread = gcnew Thread( gcnew ThreadStart( &Work::DoWork ) );
   newThread->Start();
}
using System;
using System.Threading;

class Test
{
    static void Main() 
    {
        Thread newThread = 
            new Thread(new ThreadStart(Work.DoWork));
        newThread.Start();
    }
}

class Work 
{
    Work() {}

    public static void DoWork() {}
}
open System.Threading

module Work =
    let doWork () = ()

let newThread = Thread(ThreadStart Work.doWork)
newThread.Start()
Imports System.Threading

Public Class Test
    <MTAThread> _
    Shared Sub Main()
        Dim newThread As New Thread(AddressOf Work.DoWork)
        newThread.Start()
    End Sub
End Class

Public Class Work 

    Private Sub New()
    End Sub

    Shared Sub DoWork()
    End Sub

End Class

下列程式碼範例示範如何建立執行實例方法的執行緒。

using namespace System;
using namespace System::Threading;
ref class Work
{
public:
   Work(){}

   void DoWork(){}

};

int main()
{
   Work^ threadWork = gcnew Work;
   Thread^ newThread = gcnew Thread( gcnew ThreadStart( threadWork, &Work::DoWork ) );
   newThread->Start();
}
using System;
using System.Threading;

class Test
{
    static void Main() 
    {
        Work threadWork = new Work();
        Thread newThread = 
            new Thread(new ThreadStart(threadWork.DoWork));
        newThread.Start();
    }
}

class Work 
{
    public Work() {}

    public void DoWork() {}
}
open System.Threading

type Work() =
    member _.DoWork() = ()

let threadWork = Work()
let newThread = Thread(ThreadStart threadWork.DoWork)
newThread.Start()
Imports System.Threading

Public Class Test
    <MTAThread> _
    Shared Sub Main() 
        Dim threadWork As New Work()
        Dim newThread As New Thread(AddressOf threadWork.DoWork)
        newThread.Start()
    End Sub
End Class

Public Class Work

    Sub New()
    End Sub

    Sub DoWork() 
    End Sub

End Class

備註

執行緒在建立時不會開始執行。 若要排程執行緒執行,請呼叫 Start 方法。

注意

Visual Basic 使用者可以在建立執行緒時省略 ThreadStart 建構函式。 傳遞方法時, AddressOf 請使用 運算子,例如 Dim t As New Thread(AddressOf ThreadProc) 。 Visual Basic 會自動呼叫建 ThreadStart 構函式。

另請參閱

適用於

Thread(ParameterizedThreadStart, Int32)

來源:
Thread.cs
來源:
Thread.cs
來源:
Thread.cs

初始化 Thread 類別的新執行個體,指定委派,讓物件可以在執行緒啟動時傳遞到執行緒,並指定執行緒的堆疊大小上限。

public:
 Thread(System::Threading::ParameterizedThreadStart ^ start, int maxStackSize);
public Thread (System.Threading.ParameterizedThreadStart start, int maxStackSize);
new System.Threading.Thread : System.Threading.ParameterizedThreadStart * int -> System.Threading.Thread
Public Sub New (start As ParameterizedThreadStart, maxStackSize As Integer)

參數

start
ParameterizedThreadStart

ParameterizedThreadStart 委派,代表在這個執行緒開始執行時要叫用的方法。

maxStackSize
Int32

執行緒使用的最大堆疊大小 (以位元組為單位),或是 0,使用可執行檔標頭中指定的預設最大堆疊大小。

重要 對於部分信任的程式碼,如果大於預設堆疊大小, maxStackSize 則會忽略它。 不會擲回任何例外狀況。

例外狀況

startnull

maxStackSize 小於零。

備註

避免使用此建構函式多載。 建構函式多載所使用的 Thread(ParameterizedThreadStart) 預設堆疊大小是執行緒的建議堆疊大小。 如果執行緒有記憶體問題,最可能的原因是程式設計錯誤,例如無限遞迴。

重要

從 .NET Framework 4 開始,只有完全信任的程式碼可以設定 maxStackSize 為大於預設堆疊大小的值, (1 MB) 。 如果在程式碼以部分信任執行時指定 maxStackSize 較大的值, maxStackSize 則會忽略 ,並使用預設堆疊大小。 不會擲回任何例外狀況。 任何信任層級的程式碼都可以設定 maxStackSize 為小於預設堆疊大小的值。

注意

如果您要開發完全信任的程式庫,該程式庫將由部分信任的程式碼使用,而且您必須啟動需要大型堆疊的執行緒,您必須先判斷提示完全信任,才能建立執行緒,或使用預設堆疊大小。 除非您完全控制執行緒上執行的程式碼,否則請勿這麼做。

如果 maxStackSize 小於最小堆疊大小,則會使用最小堆疊大小。 如果 maxStackSize 不是頁面大小的倍數,則會四捨五入至下一個較大的頁面大小倍數。

注意

在 Windows XP 和 Windows Server 2003 之前的 Microsoft Windows 版本上, maxStackSize 會忽略 ,並使用可執行檔標頭中指定的堆疊大小。

如果您指定非常小的堆疊大小,您可能需要停用堆疊溢位探查。 當堆疊受到嚴重限制時,探查本身可能會造成堆疊溢位。 若要停用堆疊溢位探查,請將下列內容新增至.NET Framework應用程式中的應用程式組態檔。

<configuration>
  <runtime>
    <disableStackOverflowProbing enabled="true"/>
  </runtime>
</configuration>

適用於

Thread(ThreadStart, Int32)

來源:
Thread.cs
來源:
Thread.cs
來源:
Thread.cs

初始化 Thread 類別的新執行個體,並指定執行緒的堆疊大小上限。

public:
 Thread(System::Threading::ThreadStart ^ start, int maxStackSize);
public Thread (System.Threading.ThreadStart start, int maxStackSize);
new System.Threading.Thread : System.Threading.ThreadStart * int -> System.Threading.Thread
Public Sub New (start As ThreadStart, maxStackSize As Integer)

參數

start
ThreadStart

ThreadStart 委派,代表在這個執行緒開始執行時要叫用的方法。

maxStackSize
Int32

執行緒使用的最大堆疊大小 (以位元組為單位),或是 0,使用可執行檔標頭中指定的預設最大堆疊大小。

重要 對於部分信任的程式碼,如果大於預設堆疊大小, maxStackSize 則會忽略它。 不會擲回任何例外狀況。

例外狀況

startnull

maxStackSize 小於零。

備註

避免使用此建構函式多載。 建構函式多載所使用的 Thread(ThreadStart) 預設堆疊大小是執行緒的建議堆疊大小。 如果執行緒有記憶體問題,最可能的原因是程式設計錯誤,例如無限遞迴。

重要

從 .NET Framework 4 開始,只有完全信任的程式碼可以設定 maxStackSize 為大於預設堆疊大小的值, (1 MB) 。 如果在程式碼以部分信任執行時指定 maxStackSize 較大的值, maxStackSize 則會忽略 ,並使用預設堆疊大小。 不會擲回任何例外狀況。 任何信任層級的程式碼都可以設定 maxStackSize 為小於預設堆疊大小的值。

注意

如果您要開發完全信任的程式庫,該程式庫將由部分信任的程式碼使用,而且您必須啟動需要大型堆疊的執行緒,您必須先判斷提示完全信任,才能建立執行緒,或使用預設堆疊大小。 除非您完全控制執行緒上執行的程式碼,否則請勿這麼做。

如果 maxStackSize 小於最小堆疊大小,則會使用最小堆疊大小。 如果 maxStackSize 不是頁面大小的倍數,則會四捨五入至下一個較大的頁面大小倍數。 例如,如果您在 Windows Vista 上使用 .NET Framework 2.0 版,256KB (262,144 個位元組) 是堆疊大小下限,而頁面大小為 64KB (65,536 個位元組) 。

注意

在 Windows XP 和 Windows Server 2003 之前的 Microsoft Windows 版本上, maxStackSize 會忽略 ,並使用可執行檔標頭中指定的堆疊大小。

如果您指定非常小的堆疊大小,您可能需要停用堆疊溢位探查。 當堆疊受到嚴重限制時,探查本身可能會造成堆疊溢位。 若要停用堆疊溢位探查,請將下列內容新增至.NET Framework應用程式中的應用程式組態檔。

<configuration>
  <runtime>
    <disableStackOverflowProbing enabled="true"/>
  </runtime>
</configuration>

適用於