title: When should TaskCompletionSource<T> be used?
body: <p>AFAIK, all it knows is that at some point, its <code>SetResult</code> or <code>SetException</code> method is being called to complete the <code>Task&lt;T&gt;</code> exposed through its <code>Task</code> property.</p>
  <p>In other words, it acts as the producer for a <code>Task&lt;TResult&gt;</code> and its completion.</p>
  <p>I saw <a href="https://devblogs.microsoft.com/pfxteam/the-nature-of-taskcompletionsourcetresult/" rel="noreferrer"><strong>here</strong></a> the example:</p>
  <blockquote>
  <p>If I need a way to execute a <code>Func&lt;T&gt;</code> asynchronously and have a <code>Task&lt;T&gt;</code>
  to represent that operation.</p>
  </blockquote>
  <pre><code>public static Task&lt;T&gt; RunAsync&lt;T&gt;(Func&lt;T&gt; function) 
  { 
      if (function == null) throw new ArgumentNullException(“function”); 
      var tcs = new TaskCompletionSource&lt;T&gt;(); 
      ThreadPool.QueueUserWorkItem(_ =&gt; 
      { 
          try 
          {  
              T result = function(); 
              tcs.SetResult(result);  
          } 
          catch(Exception exc) { tcs.SetException(exc); } 
      }); 
      return tcs.Task; 
  }
  </code></pre>
  <p>Which could be used if I didn’t have <code>Task.Factory.StartNew</code> -
  But I <em>do</em> have <code>Task.Factory.StartNew</code>.</p>
  <p><strong>Question:</strong></p>
  <p>Can someone please explain by example a scenario related <strong>directly</strong> to <code>TaskCompletionSource</code>
  and not to a <em>hypothetical</em> situation in which I don't have  <code>Task.Factory.StartNew</code>?</p>