title: Consuming an synchronous or asynchronous method as a Task
body: <p>I was looking at download manager type project on codeplex and came accross this one:
  <a href="http://nthdownload.codeplex.com/" rel="nofollow">http://nthdownload.codeplex.com/</a></p>
  
  <p>Browsing the code I ran across methods like <code>AddDownloads</code>, listed below:</p>
  
  <p><code>AddDownloads</code> starts the _downloadQueue.AddDownloads Task and continues with the <code>viewMaintenanceTask</code> Task.  If you look at the methods and things that happen in those 2 tasks and downstream it seems like everything is synchronous.</p>
  
  <p>Also reading this blog post, <a href="http://neverindoubtnet.blogspot.com/2012/03/synchronous-tasks-with-task.html" rel="nofollow">Synchronous tasks with Task</a> I'm trying to understand the advantage, if any, to wrapping synchronous methods in a <code>TaskCompletionSource</code>.  Is it because it gives the API consumer the option to start the task on a separate thread or simply because you want to consume the method as a <code>Task</code>.  Does a synchronous method wrapped in a <code>TaskCompletionSource</code> benefit from and parallel processing?</p>
  
  <pre><code>private Task&lt;QueueOperation&gt; AddDownloads(IEnumerable&lt;IDownload&gt; downloads, out Task&lt;QueueOperation&gt; startTask)
  {
      var addTask = _downloadQueue.AddDownloads(downloads, out startTask);
  
      // Maintain views
      var viewMaintenanceTask = addTask.ContinueWith(t =&gt;
      {
          if (t.Exception == null)
          {
              var addedDownloads = t.Result.DownloadErrors.Where(k =&gt; k.Value == null).Select(k =&gt; k.Key).ToList();
              var activeDownloads = ActiveDownloads.ToList();
              AddToActiveDownloads(addedDownloads.Except(activeDownloads).ToList(), false);
          }
          else
          {
              // Rethrow exception, this ensures it'll bubble up to any further ContinueWith chained off this task
              throw t.Exception;
          }
          return t.Result;
      });
  
      return viewMaintenanceTask;
  }
  </code></pre>
  
  <p>The example method from the blog post, wrapping a synchronous operation in a <code>TaskCompletionSource</code>:</p>
  
  <pre><code>var tcs = new TaskCompletionSource&lt;object&gt;();
  
  try
  {
      object result = model.Deserialize(stream, null, type);
      tcs.SetResult(result);
  }
  catch (Exception ex)
  {
      tcs.SetException(ex);
  }
  
  return tcs.Task;
  </code></pre>