Updated: September 2010
Represents the method that executes on a Thread.
Assembly: mscorlib (in mscorlib.dll)
<ComVisibleAttribute(False)> _
Public Delegate Sub ParameterizedThreadStart ( _
obj As Object _
)[ComVisibleAttribute(false)]
public delegate void ParameterizedThreadStart(
Object obj
)[ComVisibleAttribute(false)]
public delegate void ParameterizedThreadStart(
Object^ obj
)[<ComVisibleAttribute(false)>]
type ParameterizedThreadStart =
delegate of
obj:Object -> unitParameters
- 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
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. |
Note |
|---|
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
The following code example shows the syntax for creating and using a ParameterizedThreadStart delegate with a static method and an instance method.
Note |
|---|
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. |
Imports System
Imports System.Threading
Public Class Work
<MTAThread> _
Shared Sub Main()
' To start a thread using a shared thread procedure, use
' the class name and method name when you create the
' ParameterizedThreadStart delegate. Visual Basic expands
' the AddressOf expression to the appropriate delegate
' creation syntax:
' New ParameterizedThreadStart(AddressOf Work.DoWork)
'
Dim newThread As New Thread(AddressOf 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
' object 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. Visual
' Basic expands the AddressOf expression to the appropriate
' delegate creation syntax:
' New ParameterizedThreadStart(AddressOf w.DoMoreWork)
'
Dim w As New Work()
newThread = New Thread(AddressOf w.DoMoreWork)
' Pass an object containing data for the thread.
'
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 code example produces the following output (the order
' of the lines might vary):
'
'Static thread procedure. Data='42'
'Instance thread procedure. Data='The answer'
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'
*/
using namespace System;
using namespace System::Threading;
namespace SystemThreadingExample
{
public ref class Work
{
public:
void StartThreads()
{
// To start a thread using a shared thread procedure, use
// the class name and method name when you create the
// ParameterizedThreadStart delegate.
// AddressOf Work.DoWork)
//
Thread^ newThread = gcnew
Thread(gcnew ParameterizedThreadStart(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.
Work^ someWork = gcnew Work;
newThread =
gcnew Thread(
gcnew ParameterizedThreadStart(someWork,
&Work::DoMoreWork));
// Pass an object containing data for the thread.
//
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 code example produces the following output (the order
// of the lines might vary):
// Static thread procedure. Data='42'
// Instance thread procedure. Data='The answer'
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Note