Making Parameterized Threads in C#

Are you searching article about how to Making Parameterized Threads in C# . hereunder yo can learn about code snippet and example

Often when we start a thread, we want to give it some parameters usually some data it has to work on. The ThreadStart delegate doesn't take any parameters. So what we do if we would like to call methods of a class in Thread and that method has some parameters? In .NET 2.0, there is a new delegate, ParameterizedThreadStart, which takes a parameter of type object. You can create a thread using an instance of this delegate instead of just ThreadStart, and a new overload to Thread.Start allows you to specify the value to be passed to the new thread. This is simple, but only accepts a single parameter. There is another option in C# version 2.0 for this problem: anonymous methods. Creating anonymous methods is essentially a way to pass a code block as a delegate parameter. Basically we create a ThreadStart delegate by passing the block of code as delegate parameter. So, this option can overcome the problem of multiple parameters.

This is a demo class called Logic, which have got overloaded methods which we want to start in Thread

public class Logic
public void Call(object param)
Console.WriteLine("Call made using {0}", param);

public void Call(object param1, object param2)
Console.WriteLine("Call made using {0} & {1}", param1, param2);

Solution 1; using ParameterizedThreadStart delegate of .NET 2.0:

//We can only call the version of Call of Logic; which takes a single parameter
ParameterizedThreadStart starter = new ParameterizedThreadStart(new Logic().Call);
new Thread(starter).Start("foo");

Solution 2; using Anonymous Methods of C# 2.0:

//Put the block of code to be executed as parameter to ThreadStart delegate
ThreadStart starter2 = delegate { new Logic().Call("foo", "bar"); };
new Thread(starter2).Start();


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: