Thread 构造函数

定义

初始化 Thread 类的新实例。Initializes a new instance of the Thread class.

重载

Thread(ParameterizedThreadStart)

初始化 Thread 类的新实例,指定允许对象在线程启动时传递给线程的委托。Initializes a new instance of the Thread class, specifying a delegate that allows an object to be passed to the thread when the thread is started.

Thread(ThreadStart)

初始化 Thread 类的新实例。Initializes a new instance of the Thread class.

Thread(ParameterizedThreadStart, Int32)

初始化 Thread 类的新实例,指定允许对象在线程启动时传递给线程的委托,并指定线程的最大堆栈大小。Initializes a new instance of the Thread class, specifying a delegate that allows an object to be passed to the thread when the thread is started and specifying the maximum stack size for the thread.

Thread(ThreadStart, Int32)

初始化 Thread 类的新实例,指定线程的最大堆栈大小。Initializes a new instance of the Thread class, specifying the maximum stack size for the thread.

Thread(ParameterizedThreadStart)

初始化 Thread 类的新实例,指定允许对象在线程启动时传递给线程的委托。Initializes a new instance of the Thread class, specifying a delegate that allows an object to be passed to the thread when the thread is started.

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

一个委托,它表示此线程开始执行时要调用的方法。A delegate that represents the methods to be invoked when this thread begins executing.

异常

startnullstart is null.

示例

下面的示例演示了用静态方法和实例方法创建和使用 ParameterizedThreadStart 委托的语法。The following example shows the syntax for creating and using a ParameterizedThreadStart delegate with a static method and an instance method.

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

注解

创建线程时,它不会开始执行。A thread does not begin executing when it is created. 若要计划线程的执行,请调用 Start 方法。To schedule the thread for execution, call the Start method. 若要将数据对象传递到线程,请使用 Start(Object) 方法重载。To pass a data object to the thread, use the Start(Object) method overload.

备注

Visual Basic 用户在创建线程时可以省略 ThreadStart 构造函数。Visual Basic users can omit the ThreadStart constructor when creating a thread. 传递方法时,请使用 AddressOf 运算符,例如 Dim t As New Thread(AddressOf ThreadProc)Use the AddressOf operator when passing your method, for example Dim t As New Thread(AddressOf ThreadProc). Visual Basic 自动调用 ThreadStart 构造函数。Visual Basic automatically calls the ThreadStart constructor.

另请参阅

Thread(ThreadStart)

初始化 Thread 类的新实例。Initializes a new instance of the Thread class.

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 委托。A ThreadStart delegate that represents the methods to be invoked when this thread begins executing.

异常

start 参数为 nullThe start parameter is null.

示例

下面的代码示例演示如何创建执行静态方法的线程。The following code example shows how to create a thread that executes a static method.

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() {}
}
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

下面的代码示例演示如何创建执行实例方法的线程。The following code example shows how to create a thread that executes an instance method.

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() {}
}
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

注解

创建线程时,它不会开始执行。A thread does not begin executing when it is created. 若要计划线程的执行,请调用 Start 方法。To schedule the thread for execution, call the Start method.

备注

Visual Basic 用户在创建线程时可以省略 ThreadStart 构造函数。Visual Basic users can omit the ThreadStart constructor when creating a thread. 传递方法时,请使用 AddressOf 运算符,例如 Dim t As New Thread(AddressOf ThreadProc)Use the AddressOf operator when passing your method for example Dim t As New Thread(AddressOf ThreadProc). Visual Basic 自动调用 ThreadStart 构造函数。Visual Basic automatically calls the ThreadStart constructor.

另请参阅

Thread(ParameterizedThreadStart, Int32)

初始化 Thread 类的新实例,指定允许对象在线程启动时传递给线程的委托,并指定线程的最大堆栈大小。Initializes a new instance of the Thread class, specifying a delegate that allows an object to be passed to the thread when the thread is started and specifying the maximum stack size for the 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 委托。A ParameterizedThreadStart delegate that represents the methods to be invoked when this thread begins executing.

maxStackSize
Int32

线程要使用的最大堆栈大小(以字节为单位);如果为 0,则使用可执行文件的文件头中指定的默认最大堆栈大小。The maximum stack size, in bytes, to be used by the thread, or 0 to use the default maximum stack size specified in the header for the executable.

重要事项:对于部分受信任的代码,如果 maxStackSize 大于默认堆栈大小,则会将其忽略。Important For partially trusted code, maxStackSize is ignored if it is greater than the default stack size. 不引发异常。No exception is thrown.

异常

startnullstart is null.

maxStackSize 小于零。maxStackSize is less than zero.

注解

避免使用此构造函数重载。Avoid using this constructor overload. Thread(ParameterizedThreadStart) 构造函数重载使用的默认堆栈大小是线程的建议堆栈大小。The default stack size used by the Thread(ParameterizedThreadStart) constructor overload is the recommended stack size for threads. 如果线程存在内存问题,最可能的原因是编程错误,如无限递归。If a thread has memory problems, the most likely cause is programming error, such as infinite recursion.

重要

.NET Framework 4.NET Framework 4开始,只有完全受信任的代码才能将 maxStackSize 设置为大于默认堆栈大小(1 mb)的值。Beginning with the .NET Framework 4.NET Framework 4, only fully trusted code can set maxStackSize to a value that is greater than the default stack size (1 megabyte). 如果为 maxStackSize 指定较大的值,则当代码在部分信任的情况下运行时,将忽略 maxStackSize 并使用默认堆栈大小。If a larger value is specified for maxStackSize when code is running with partial trust, maxStackSize is ignored and the default stack size is used. 不引发异常。No exception is thrown. 任何信任级别的代码都可以将 maxStackSize 设置为小于默认堆栈大小的值。Code at any trust level can set maxStackSize to a value that is less than the default stack size.

备注

如果你正在开发将由部分信任的代码使用的完全受信任的库,并且你需要启动需要大型堆栈的线程,则必须在创建该线程之前断言完全信任,否则将使用默认堆栈大小。If you are developing a fully trusted library that will be used by partially trusted code, and you need to start a thread that requires a large stack, you must assert full trust before creating the thread, or the default stack size will be used. 除非你完全控制在线程上运行的代码,否则不要执行此操作。Do not do this unless you fully control the code that runs on the thread.

如果 maxStackSize 小于最小堆栈大小,则使用最小堆栈大小。If maxStackSize is less than the minimum stack size, the minimum stack size is used. 如果 maxStackSize 不是页面大小的倍数,则会将其舍入到页面大小的下一个较大倍数。If maxStackSize is not a multiple of the page size, it is rounded to the next larger multiple of the page size. 例如,如果你使用的是 Windows Vista 上的 .NET Framework 2.0 版,256KB (262144字节)是最小堆栈大小,而页面大小为64KB (65536个字节)。For example, if you are using the .NET Framework version 2.0 on Windows Vista, 256KB (262,144 bytes) is the minimum stack size, and the page size is 64KB (65,536 bytes).

备注

在 Windows XP 和 Windows Server 2003 之前的 Microsoft Windows 版本上,将忽略 maxStackSize,并使用可执行文件头中指定的堆栈大小。On versions of Microsoft Windows prior to Windows XP and Windows Server 2003, maxStackSize is ignored, and the stack size specified in the executable header is used.

如果指定非常小的堆栈大小,则可能需要禁用堆栈溢出探测。If you specify a very small stack size, you might need to disable stack-overflow probing. 当堆栈受到严格约束时,探测本身就会导致堆栈溢出。When the stack is severely constrained, the probing can itself cause a stack overflow. 若要禁用堆栈溢出探测,请将以下项添加到应用程序配置文件中。To disable stack overflow probing, add the following to your application configuration file.

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

Thread(ThreadStart, Int32)

初始化 Thread 类的新实例,指定线程的最大堆栈大小。Initializes a new instance of the Thread class, specifying the maximum stack size for the 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 委托。A ThreadStart delegate that represents the methods to be invoked when this thread begins executing.

maxStackSize
Int32

线程要使用的最大堆栈大小(以字节为单位);如果为 0,则使用可执行文件的文件头中指定的默认最大堆栈大小。The maximum stack size, in bytes, to be used by the thread, or 0 to use the default maximum stack size specified in the header for the executable.

重要事项:对于部分受信任的代码,如果 maxStackSize 大于默认堆栈大小,则会将其忽略。Important For partially trusted code, maxStackSize is ignored if it is greater than the default stack size. 不引发异常。No exception is thrown.

异常

startnullstart is null.

maxStackSize 小于零。maxStackSize is less than zero.

注解

避免使用此构造函数重载。Avoid using this constructor overload. Thread(ThreadStart) 构造函数重载使用的默认堆栈大小是线程的建议堆栈大小。The default stack size used by the Thread(ThreadStart) constructor overload is the recommended stack size for threads. 如果线程存在内存问题,最可能的原因是编程错误,如无限递归。If a thread has memory problems, the most likely cause is programming error, such as infinite recursion.

重要

.NET Framework 4.NET Framework 4开始,只有完全受信任的代码才能将 maxStackSize 设置为大于默认堆栈大小(1 mb)的值。Beginning with the .NET Framework 4.NET Framework 4, only fully trusted code can set maxStackSize to a value that is greater than the default stack size (1 megabyte). 如果为 maxStackSize 指定较大的值,则当代码在部分信任的情况下运行时,将忽略 maxStackSize 并使用默认堆栈大小。If a larger value is specified for maxStackSize when code is running with partial trust, maxStackSize is ignored and the default stack size is used. 不引发异常。No exception is thrown. 任何信任级别的代码都可以将 maxStackSize 设置为小于默认堆栈大小的值。Code at any trust level can set maxStackSize to a value that is less than the default stack size.

备注

如果你正在开发将由部分信任的代码使用的完全受信任的库,并且你需要启动需要大型堆栈的线程,则必须在创建该线程之前断言完全信任,否则将使用默认堆栈大小。If you are developing a fully trusted library that will be used by partially trusted code, and you need to start a thread that requires a large stack, you must assert full trust before creating the thread, or the default stack size will be used. 除非你完全控制在线程上运行的代码,否则不要执行此操作。Do not do this unless you fully control the code that runs on the thread.

如果 maxStackSize 小于最小堆栈大小,则使用最小堆栈大小。If maxStackSize is less than the minimum stack size, the minimum stack size is used. 如果 maxStackSize 不是页面大小的倍数,则会将其舍入到页面大小的下一个较大倍数。If maxStackSize is not a multiple of the page size, it is rounded to the next larger multiple of the page size. 例如,如果你使用的是 Windows Vista 上的 .NET Framework 2.0 版,256KB (262144字节)是最小堆栈大小,而页面大小为64KB (65536个字节)。For example, if you are using the .NET Framework version 2.0 on Windows Vista, 256KB (262,144 bytes) is the minimum stack size, and the page size is 64KB (65,536 bytes).

备注

在 Windows XP 和 Windows Server 2003 之前的 Microsoft Windows 版本上,将忽略 maxStackSize,并使用可执行文件头中指定的堆栈大小。On versions of Microsoft Windows prior to Windows XP and Windows Server 2003, maxStackSize is ignored, and the stack size specified in the executable header is used.

如果指定非常小的堆栈大小,则可能需要禁用堆栈溢出探测。If you specify a very small stack size, you might need to disable stack-overflow probing. 当堆栈受到严格约束时,探测本身就会导致堆栈溢出。When the stack is severely constrained, the probing can itself cause a stack overflow. 若要禁用堆栈溢出探测,请将以下项添加到应用程序配置文件中。To disable stack overflow probing, add the following to your application configuration file.

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

适用于