Export (0) Print
Expand All
Expand Minimize

ParameterizedThreadStart Delegate

Represents the method that executes on a Thread.

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

[ComVisibleAttribute(false)]
public delegate void ParameterizedThreadStart(
	Object obj
)

Parameters

obj
Type: System.Object

An object that contains data for the thread procedure.

When a managed thread is created, the method that executes on the thread is represented by a ThreadStart delegate or a ParameterizedThreadStart delegate that is passed to the Thread constructor. The thread does not begin executing until the Thread.Start method is called. The ThreadStart or ParameterizedThreadStart delegate is invoked on the thread, and execution begins at the first line of the method represented by the delegate. In the case of the ParameterizedThreadStart delegate, the object that is passed to the Start(Object) method is passed to the delegate.

NoteNote

Visual Basic and C# users can omit the ThreadStart or ParameterizedThreadStart delegate constructor when creating a thread. In Visual Basic, use the AddressOf operator when passing your method to the Thread constructor; for example, Dim t As New Thread(AddressOf ThreadProc). In C#, simply specify the name of the thread procedure. The compiler selects the correct delegate constructor.

NoteNote

When you create a ParameterizedThreadStart delegate for an instance method in C++, the first parameter of the constructor is the instance variable. For a static method, the first parameter of the constructor is zero. For a static method, the delegate constructor requires only one parameter: the address of the callback method, qualified by the class name.

The ParameterizedThreadStart delegate and the Thread.Start(Object) method overload make it easy to pass data to a thread procedure, but this technique is not type safe because any object can be passed to Thread.Start(Object). A more robust way to pass data to a thread procedure is to put both the thread procedure and the data fields into a worker object. For more information, see Creating Threads and Passing Data at Start Time.

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

NoteNote

The Visual Basic and C# compilers infer the ParameterizedThreadStart delegate from the signatures of the DoWork and DoMoreWork methods, and call the correct constructor. Thus, there is no explicit constructor call in the code.

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