2 out of 4 rated this helpful - Rate this topic

ParameterizedThreadStart Delegate

Updated: September 2010

Represents the method that executes on a Thread.

Namespace:  System.Threading
Assembly:  mscorlib (in mscorlib.dll)
[ComVisibleAttribute(false)]
public delegate void ParameterizedThreadStart(
	Object obj
)

Parameters

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.Start 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(Object) method is passed to the delegate.

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

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


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'
*/


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

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.

Date

History

Reason

September 2010

Clarified that the example uses ParameterizedThreadStart.

Customer feedback.

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Terrible example

This method of ParameterizedThreadStart ONLY works for static methods. No reason not to also have an example for non static methods other than lazy.

Perhaps the example description could be more clear

The description of the example says it shows both a static method and an instance method, but doesn't name the methods. The static method is DoWork, and the instance method is DoMoreWork.

Glenn Hackney
Common Language Runtime Developer Guidance

Why does this work?
In a static class on my main thread, I setup a new thread.

  if (GameManager.Gamestate == GameState.createmap)
{
Thread mythread = new Thread(start_createmap);
mythread.SetApartmentState(ApartmentState.STA);
titleform.Hide();
mythread.Start();
mythread.Join();
titleform.Show();
}
// here is the thread it starts
#region Threadstart functions
private static void start_createmap()
{
F_Mapeditor fmap = new F_Mapeditor(titleform);
// Titleform is a Static Var on the main thread
fmap.thegame = new Mapeditor(fmap.pictureBox.Handle, fmap, fmap.pictureBox);
fmap.Show();
fmap.thegame.Run();
}



I thought that because this function was called on a new thread (after the thread start call) I should not be able to see the calling class vars ? So what is really going on here ? I am confused. thanks, Fleanbilly

This is a matter of scope, and the rules of scope apply to any thread

Good question. The thread function is a static method of the class, so any class-level static field is in scope. This is true regardless of which thread happens to be running the start_createmap function.

Glenn Hackney
Common Language Runtime Developer Guidance

Delegate Signature
Since I didn't read carefully the documentation , there's one thing that took me a time to realize about starting threads with parameters: they must be of type "object" and then casted inside.
So the signature would be, for example, "protected void process(object client)", and inside the body the client is casted to TcpClient.

The most useless example ever? Maybe not.
Is it just me or does the C# example have nothing to do with the ParameterizedThreadStart?

[gh] Good question! This is a case of the C# and VB compilers hiding things to make our lives easier. The compilers notice the signature of the Work.DoWork method, and select the Thread constructor that takes a delegate with that signature. In this case, as the code comment points out, the compiler selects the Thread constructor that takes a ParameterizedThreadStart delegate. You don't have to type "new ParameterizedThreadStart(...)" because the compiler does that for you, under the covers.

Thx for asking; I'm sure others have wondered the same thing. This really should be mentioned in the sample description, not just in the code comments. (Also, I see a copy/paste error in the C++ comments that should be fixed.) -- Glenn