I start a few parallel tasks, like this:
var tasks =
Enumerable.Range(1, 500)
.Select(i => Task.Factory.StartNew<int>(ProduceSomeMagicIntValue))
.ToArray();
and then join them with
Task.WaitAll(tasks);
On this last line I get a blue squiggly marker under tasks
, with a warning message:
Co-variant array conversion from Task[] to Task[]
can cause run-time exception on write operation.
I understand why I get this message, but is there a way around that? (for example, like a generic version of Task.WaitAll()
?)
WaitAll()
won't write to the array. Is there any reason why you want to avoid it?Task.WhenAll()
that returns a singleTask
that completes when all theTask
s in the collection complete. And it also has a generic version and it works on anyIEnumerable<T>
ofTask
s.