[...] is there a better way? This seems like a very ugly way to do it.
Yes, this is a very ugly and inefficient way to do this. Each thread will consume a lot of resources, but your computer can only execute N threads at a time, where N is the number of processor cores on your computer. Therefore, instead of using a low-level primitive, such as a thread, you can create on top of libraries that optimize the number of threads to match the number of processor cores.
In your case, when you have a collection of objects, I suggest:
LINQ Code:
var result = source.Select(item => ...).ToList();
LINQ parallel code:
var result = source.AsParallel().Select(item => ...).ToList();
For each cycle:
foreach (var item in source)
Process(item);
In parallel for each cycle:
Parallel.ForEach(
source,
item => Process(item);
);
For the loop:
for (var i = 0; i < list.Count; i += 1)
list[i] = Process(list[i]);
Parallel loop:
Parallel.For(
0,
list.Count,
i => {
list[i] = Process(list[i]);
}
);
source
share