I'm trying to pass parameters through the following:
Thread thread = new Thread(new ParameterizedThreadStart(DoMethod));
Any idea how to do this? I'd appreciate some help
C# | Thread(ParameterizedThreadStart) Constructor Thread(ParameterizedThreadStart) Constructor is used to initialize a new instance of the Thread class. It defined a delegate which allows an object to pass to the thread when the thread starts.
Thread pool in C# is a collection of threads. It is used to perform tasks in the background. When a thread completes a task, it is sent to the queue wherein all the waiting threads are present. This is done so that it can be reused.
Create New Thread [C#] First, create a new ThreadStart delegate. The delegate points to a method that will be executed by the new thread. Pass this delegate as a parameter when creating a new Thread instance. Finally, call the Thread.
lazyberezovsky has the right answer. I want to note that technically you can pass an arbitrary number of arguments using lambda expression due to variable capture:
var thread = new Thread( () => DoMethod(a, b, c)); thread.Start();
This is a handy way of calling methods that don't fit the ThreadStart
or ParameterizedThreadStart
delegate, but be careful that you can easily cause a data race if you change the arguments in the parent thread after passing them to the child thread's code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With