ParameterizedThreadStart Delegate
Assembly: mscorlib (in mscorlib.dll)
'Declaration <ComVisibleAttribute(False)> _ Public Delegate Sub ParameterizedThreadStart ( _ obj As Object _ ) 'Usage Dim instance As New ParameterizedThreadStart(AddressOf HandlerMethod)
/** @delegate */ /** @attribute ComVisibleAttribute(false) */ public delegate void ParameterizedThreadStart ( Object obj )
JScript supports the use of delegates, but not the declaration of new ones.
Parameters
- obj
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 System.Threading.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. |
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.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.
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(New ParameterizedThreadStart(AddressOf w.DoMoreWork)) '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'
Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.
Note