title: Waiting for Parallel.For
body: <p>I know that there is no need to wait for a <code>Parallel.For</code> but since UI Windows messages (WinForms message pump) are being processed during the <code>Parallel.For</code> I would like to wait for <code>Parallel.For</code> to finish.</p>
  
  <p>Is it a good idea to encapsulate a <code>Parallel.For</code> inside a <code>Task</code>, then wait for it? Is there any better approach?</p>
  
  <p>Thanks.</p>
  
  <pre><code>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;
  }
  </code></pre>