I have, let's say, a list with 500 objects. For each object, I'm calling a function calculating it's cost. So each of the 500 calls is independent from the others. The overall takes around 30 seconds. Wouldn't it be possible to run all the 500 tasks at the same time as they don't rely on each other ? I know nothing about multi-threading therefore I don't know if it could be a solution.
You can easily parallelize the work using the Parallel.ForEach Method:
Parallel.ForEach(items, item =>
{
item.CalculateCost();
});
Running a single threaded process will only use one-core of your machine (this does allow other cores to run operating system and other application processes).
Your process sounds liek a good contender for multi-threaded processing, however you don't need a new thread for every process - this will create overhead in creating the threads, and also you won't have enough cores to run them all individually, so they will be fighting for CPU resources.
Using Parallel.For in .Net4.0 will cleverly use as many threads as it can.
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