Export (0) Print
Expand All

Thread Constructor (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.

Namespace:  System.Threading
Assembly:  mscorlib (in mscorlib.dll)

public Thread(
	ParameterizedThreadStart start
)

Parameters

start
Type: System.Threading.ParameterizedThreadStart

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

ExceptionCondition
ArgumentNullException

start is null.

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.

NoteNote

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.

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

using System;
using System.Threading;

public class Work
{
    public static void Main()
    {
        // To start a thread using a shared thread procedure, use 
        // the class name and method name when you create the  
        // ParameterizedThreadStart delegate. C# infers the  
        // appropriate delegate creation syntax: 
        //    new ParameterizedThreadStart(Work.DoWork) 
        //
        Thread newThread = new Thread(Work.DoWork);

        // Use the overload of the Start method that has a 
        // parameter of type Object. You can create an object that 
        // contains several pieces of data, or you can pass any  
        // reference type or value type. The following code passes 
        // the integer value 42. 
        //
        newThread.Start(42);

        // To start a thread using an instance method for the thread  
        // procedure, use the instance variable and method name when  
        // you create the ParameterizedThreadStart delegate. C# infers  
        // the appropriate delegate creation syntax: 
        //    new ParameterizedThreadStart(w.DoMoreWork) 
        //
        Work w = new Work();
        newThread = new Thread(w.DoMoreWork);

        // Pass an object containing data for the thread. 
        //
        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 code example produces the following output (the order 
   of the lines might vary):

Static thread procedure. Data='42'
Instance thread procedure. Data='The answer'
*/

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft