ParameterizedThreadStart Delegate
 

Represents the method that executes on a Thread.

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

Syntax
[ComVisibleAttribute(false)]
public delegate void ParameterizedThreadStart(
	object obj
)
[ComVisibleAttribute(false)]
public delegate void ParameterizedThreadStart(
	Object^ obj
)
[<ComVisibleAttribute(false)>]
type ParameterizedThreadStart = 
    delegate of 
        obj:Object -> unit
<ComVisibleAttribute(False)>
Public Delegate Sub ParameterizedThreadStart (
	obj As Object
)

Parameters

obj
Type:

An object that contains data for the thread procedure.

Remarks

When a managed thread is created, the method that executes on the thread is represented by:

  • A ThreadStart delegate that is passed to the ThreadThread constructor. Any method that has no parameters and that returns void in C# or is a Sub procedure in Visual Basic can represent the delegate.

  • A ParameterizedThreadStart delegate that is passed to the ThreadThread constructor. Any method that has a single parameter of type Object and that returns void in C# or is a Sub procedure in Visual Basic can represent the delegate.

The thread does not begin executing until the ThreadStart 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 method is passed to the delegate.

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

System_CAPS_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 ThreadStart 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 ThreadStart. 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 ParameterizedThreadStart delegate supports only a single parameter. You can pass multiple data items to the ParameterizedThreadStart by making that parameter one of the following:

  • An array.

  • A collection type, if all of the data items are of the same type.

  • A tuple type, such as Tuple or Tuple.

Examples

The following code example uses a ParameterizedThreadStart delegate with to execute a static method and an instance method. The first ParameterizedThreadStart delegate is represented by the static DoWork method and the second is represented by the instance DoMoreWork method. Both methods match the ParameterizedThreadStart delegate signature; that is, they have a single parameter of type Object and don't return a value.

System_CAPS_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()
    {
        // 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.'
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.'
Version Information
.NET Framework
Available since 2.0
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Return to top