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

Exception Condition
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.

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

.NET Framework
Available since 2.0
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Return to top
Show: