title: Show form at task completion in UI thread
body: <p>I have a task which, when completed, is supposed to continue with another task that shows a winform (the winform was previously initialised on the UI thread, so it does have a handle).</p>
  
  <pre><code>    private static Task RunningTask
      {
          get;
          set;
      }
      public static UpdaterTool.Forms.UpdateResult UpdateResultForm;
  
      private void DoWork()
      {
          UpdateResultForm = new Forms.UpdateResult(); 
          //the next line forces the creation of the handle - 
          //otherwise InvokeRequired will later on return false.
          var hackHandle = UpdateResultForm.Handle; 
  
          var ctx = TaskScheduler.FromCurrentSynchronizationContext();
  
          RunningTask = Task.Factory.StartNew(() =&gt; DownloadAndInstallFiles(), CancelTokenSource.Token)
              .ContinueWith(_ =&gt; WorkComplete(), CancelTokenSource.Token, TaskContinuationOptions.NotOnFaulted, ctx);
      }
  
      private void WorkComplete()
      {
         ShowResultForm();
      }
  
      private void ShowResultForm()
      {
          if (UpdateResultForm.InvokeRequired)
          {
              try
              {
                  UpdateResultForm.Invoke(new MethodInvoker(ShowResultForm));
              }
              catch { }
              return;
           }
           UpdateResultForm.Show();
  
       }
  </code></pre>
  
  <p>The problem is that no matter what combination of overloads for the ContinueWith() I use, the UpdateResultForm is either not shown at all(meaning the continuation does not happen, the worker hangs at "running"), or when it is, it hangs the UI, like its expecting the worker thread to finish or something. I dont understand why this happens when I tried to show it on the UI thread, by using FromCurrentSynchronizationContext().</p>
  
  <p>In my understanding, inside the DoWork method I start on the UI thread (which is why I initialise the form there). When the code enters Task.Factory.StartNew, it switches to the working thread. When it completes, it continues with WorkComplete which just shows the previously initialised form, on the UI thread. </p>
  
  <p>What am I missing? Thanks,</p>