Reputation: 363
I am searching for a solution where I can add multiple method calls to an thread. A normal Thread call only be started with one method call, i would like to add multiple independent methods calls. It could be thousands of methods to be called from a thread so a thread for every method call is not possible. I thought of for example 10 threads which are getting methods to call. Or is a threadpool the better approach?
Something like this would be nice:
Thread t1 = new Thread();
Thread t2 = new Thread();
t1.Add(DoWork(1));
t1.Add(DoWork(2));
t2.Add(DoWork(3));
t2.Add(DoWork(4));
t1.Start(); t2.Start();
Upvotes: 0
Views: 1837
Reputation: 40928
Start the thread with one method that calls all the other methods. This can be simplified by using an anonymous function:
Thread t1 = new Thread(() => {
DoWork(1);
DoWork(2);
});
Upvotes: 3
Reputation: 685
You can just create a function which calls all those methods, and then pass that to the thread. But you should really avoid using raw threads if you can. If all these are just independent operations which need to be performed, why not just run them as Task
s?
Upvotes: 0