ThreadStart Delegate
Represents the method that executes on a Thread.
Assembly: mscorlib (in mscorlib.dll)
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. Execution begins at the first line of the method represented by the ThreadStart or ParameterizedThreadStart delegate.
Note |
|---|
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. |
[C++]
Note |
|---|
In version 2.0 of the .NET Framework, creating a ThreadStart delegate for a static method in C++ requires only one parameter: the address of the callback method, qualified by the class name. In earlier versions two parameters were required when creating a delegate for a static method: zero (null) and the method address. For an instance method, all versions require two parameters: the instance variable and the method address. |
The following code example shows the syntax for creating and using a ThreadStart delegate with an instance method and with a static method.
For another simple example that demonstrates how to create a ThreadStart delegate, see the Thread::Start() method overload. For more information about thread creation, see Creating Threads and Passing Data at Start Time.
using namespace System; using namespace System::Threading; ref class Work { public: static void DoWork() { Console::WriteLine( "Static thread procedure." ); } int Data; void DoMoreWork() { Console::WriteLine( "Instance thread procedure. Data={0}", Data ); } }; int main() { // To start a thread using an instance method for the thread // procedure, specify the object as the first argument of the // ThreadStart constructor. // Work^ w = gcnew Work; w->Data = 42; ThreadStart^ threadDelegate = gcnew ThreadStart( w, &Work::DoMoreWork ); Thread^ newThread = gcnew Thread( threadDelegate ); newThread->Start(); // To start a thread using a static thread procedure, specify // only the address of the procedure. This is a change from // earlier versions of the .NET Framework, which required // two arguments, the first of which was null (0). // threadDelegate = gcnew ThreadStart( &Work::DoWork ); newThread = gcnew Thread( threadDelegate ); newThread->Start(); } /* This code example produces the following output (the order of the lines might vary): Static thread procedure. Instance thread procedure. Data=42 */
Available since 1.1
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
