How to pass parameters to ThreadStart method in Thread?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
In C#, the ThreadStart type represents the main entry point for a thread, specifically aiming to encapsulate a method that takes no parameters and returns no value. However, this lack of direct parameter passing can sometimes limit the flexibility of threading use cases. To overcome these limitations and successfully pass parameters to a method invoked by an instance of ThreadStart, alternative strategies must be employed.
Understanding Parameter Passing in Threads
Thread methods often require user input or some form of data to process, making the conventional ThreadStart delegate, which accepts only a parameterless method, somewhat limited. To circumvent this, it's essential to adopt methods like:
- Lambda Expressions or Anonymous Delegates: This involves capturing the parameters in a closure.
- ParameterizedThreadStart: A specialized delegate that allows parameter passing.
- Using State Objects: Specify a class or a
structto encapsulate method parameters. - ThreadLocal Storage: Use of
ThreadLocal<T>to store data local to a thread.
Let's delve into each method with examples and technical explanations.
1. Lambda Expressions or Anonymous Delegates
Lambda expressions or anonymous delegates provide an elegant way to pass parameters to a thread. The trick is to leverage the power of closures, thereby enclosing parameter values within their scope.
Explanation: The lambda expression () => ProcessData(someParameter) encapsulates someParameter within its closure, thereby allowing the ProcessData method to be called with the necessary argument.
2. ParameterizedThreadStart
ParameterizedThreadStart is a delegate that represents a method with a single parameter of object type.
Explanation: Here, the ProcessData method takes an object parameter, which can be cast to the required specific type, enabling parameter passing through the Thread.Start method.
3. Using State Objects
When multiple parameters are required, encapsulating them in an object for passage to the thread is a practical approach.
Explanation: Here, ThreadState is a state object carrying multiple parameters, cast and used within the ProcessData method.
4. ThreadLocal Storage
ThreadLocal<T> allows each thread to have its separate instance of a variable.
Explanation: ThreadLocal<int> initializes a unique data instance specific to a thread, which can be accessed through threadLocalData.Value.
Summary Table of Parameter Passing Methods
| Method | Description | Use Case |
| Lambda Expression | Utilizes closures to encapsulate parameters. | Simple parameters, cleaner syntax |
| ParameterizedThreadStart | Accepts a single object parameter | Suitable for one parameter |
| State Objects | Encapsulate multiple parameters in a class or struct | Complex data structures |
| ThreadLocal Storage | Stores and isolates data specific to individual threads | When thread-specific data is needed |
Each of these strategies offers a distinct advantage and scenario suitability, making thread parameterization more flexible and comprehensive.
In conclusion, while C#'s ThreadStart does not directly support parameterized methods, by employing these techniques—lambda expressions, ParameterizedThreadStart, encapsulating state objects, and ThreadLocal storage—we can effectively pass the desired parameters to threads, thereby enhancing the capability and usability of multi-threaded applications.
Related reading
- How to pass Task results to other Tasks not using continuations
- How to pass the UI Dispatcher to the ViewModel
- How to pause / sleep thread or process in Android?
- How to Pause and Resume the Task which runs Asynchronously
- How to play a sound in C, .NET
- How to preserve HttpContext in Web API async task
- How to perform an async task against es6 generators in loop
- How to perform async initalization of lazy injection

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.