Thread Constructors

Definition

Initializes a new instance of the Thread class.

Overloads

Thread(ParameterizedThreadStart)

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)

Initializes a new instance of the Thread class.

Thread(ParameterizedThreadStart, Int32)

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)

Initializes a new instance of the Thread class, specifying the maximum stack size for the thread.

Thread(ParameterizedThreadStart)

Source:
Thread.cs
Source:
Thread.cs
Source:
Thread.cs

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.

C#
public Thread(System.Threading.ParameterizedThreadStart start);

Parameters

start
ParameterizedThreadStart

A delegate that represents the methods to be invoked when this thread begins executing.

Exceptions

start is null.

Examples

The following example shows the syntax for creating and using a ParameterizedThreadStart delegate with a static method and an instance method.

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

Remarks

A thread does not begin executing when it is created. To schedule the thread for execution, call the Start method. To pass a data object to the thread, use the Start(Object) method overload.

Note

Visual Basic users can omit the ThreadStart constructor when creating a thread. Use the AddressOf operator when passing your method, for example, Dim t As New Thread(AddressOf ThreadProc). Visual Basic automatically calls the ThreadStart constructor.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Thread(ThreadStart)

Source:
Thread.cs
Source:
Thread.cs
Source:
Thread.cs

Initializes a new instance of the Thread class.

C#
public Thread(System.Threading.ThreadStart start);

Parameters

start
ThreadStart

A ThreadStart delegate that represents the methods to be invoked when this thread begins executing.

Exceptions

The start parameter is null.

Examples

The following code example shows how to create a thread that executes a static method.

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

The following code example shows how to create a thread that executes an instance method.

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

Remarks

A thread does not begin executing when it is created. To schedule the thread for execution, call the Start method.

Note

Visual Basic users can omit the ThreadStart constructor when creating a thread. Use the AddressOf operator when passing your method, for example, Dim t As New Thread(AddressOf ThreadProc). Visual Basic automatically calls the ThreadStart constructor.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Thread(ParameterizedThreadStart, Int32)

Source:
Thread.cs
Source:
Thread.cs
Source:
Thread.cs

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.

C#
public Thread(System.Threading.ParameterizedThreadStart start, int maxStackSize);

Parameters

start
ParameterizedThreadStart

A ParameterizedThreadStart delegate that represents the methods to be invoked when this thread begins executing.

maxStackSize
Int32

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.

Important For partially trusted code, maxStackSize is ignored if it is greater than the default stack size. No exception is thrown.

Exceptions

start is null.

maxStackSize is less than zero.

Remarks

Avoid using this constructor overload. 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.

Important

Beginning with the .NET Framework 4, only fully trusted code can set maxStackSize to a value that is greater than the default stack size (1 megabyte). 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. Code at any trust level can set maxStackSize to a value that is less than the default stack size.

Note

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.

If maxStackSize is less than the minimum stack size, the minimum stack size is used. If maxStackSize is not a multiple of the page size, it is rounded to the next larger multiple of the page size.

Note

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 in a .NET Framework app.

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

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Thread(ThreadStart, Int32)

Source:
Thread.cs
Source:
Thread.cs
Source:
Thread.cs

Initializes a new instance of the Thread class, specifying the maximum stack size for the thread.

C#
public Thread(System.Threading.ThreadStart start, int maxStackSize);

Parameters

start
ThreadStart

A ThreadStart delegate that represents the methods to be invoked when this thread begins executing.

maxStackSize
Int32

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.

Important For partially trusted code, maxStackSize is ignored if it is greater than the default stack size. No exception is thrown.

Exceptions

start is null.

maxStackSize is less than zero.

Remarks

Avoid using this constructor overload. 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.

Important

Beginning with the .NET Framework 4, only fully trusted code can set maxStackSize to a value that is greater than the default stack size (1 megabyte). 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. Code at any trust level can set maxStackSize to a value that is less than the default stack size.

Note

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.

If maxStackSize is less than the minimum stack size, the minimum stack size is used. If maxStackSize is not a multiple of the page size, it is rounded to the next larger multiple of the page size. 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).

Note

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 in a .NET Framework app.

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

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1