Thread 构造函数

定义

初始化 Thread 类的新实例。

重载

Thread(ParameterizedThreadStart)

初始化 Thread 类的新实例,指定允许对象在线程启动时传递给线程的委托。

Thread(ThreadStart)

初始化 Thread 类的新实例。

Thread(ParameterizedThreadStart, Int32)

初始化 Thread 类的新实例,指定允许对象在线程启动时传递给线程的委托,并指定线程的最大堆栈大小。

Thread(ThreadStart, Int32)

初始化 Thread 类的新实例,指定线程的最大堆栈大小。

Thread(ParameterizedThreadStart)

Source:
Thread.cs
Source:
Thread.cs
Source:
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)

Source:
Thread.cs
Source:
Thread.cs
Source:
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)

Source:
Thread.cs
Source:
Thread.cs
Source:
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 兆字节) 的值。 如果在使用部分信任的情况下为 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)

Source:
Thread.cs
Source:
Thread.cs
Source:
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 兆字节) 的值。 如果在使用部分信任的情况下为 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>

适用于