var tasks = new List<Task>();
Parallel.ForEach(
{
tasks.Add(...);
});
This use of List<Task> tasks
is not thread-safe. You are seeing null
elements where they shouldn't be but run this a little longer and you may see other symptoms and exceptions as well. The behavior is undefined.
Protect the access to tasks
, replace it with a ConcurrentBag
or, my choice, drop those Tasks altogether. You are getting enough parallelism from the Parallel.ForEach()
.