I know that there is no need to wait for a Parallel.For
but since UI Windows messages (WinForms message pump) are being processed during the Parallel.For
I would like to wait for Parallel.For
to finish.
Is it a good idea to encapsulate a Parallel.For
inside a Task
, then wait for it? Is there any better approach?
Thanks.
CancellationTokenSource token = new CancellationTokenSource();
const int len = 100;
double[] array = new double[len];
Task t = Task.Factory.StartNew(delegate {
Parallel.For(0, len, delegate(int i, ParallelLoopState loopState) {
array[i] += 1;
});
try
{
t.Wait(token.Token);
}
catch (OperationCanceledException e)
{
rc = false;
}
Instead of Parallel.For
why not use just Task
and call Task.WaitAll()
?
var t1 = Task.Run(() => {});
var t2 = Task.Run(() => {});
Task.WaitAll(t1, t2);
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