title: What is the difference between Task.Run and Task.Factory.StartNew
body: <p>I know this question has been asked before, but I didn't get the right answer after googling SO. </p>
  
  <p>I have these lines of code:</p>
  
  <pre><code>Task.Run(() =&gt; DoSomething())
      .ContinueWith(t=&gt;Log.Error(t,"Error"), TaskContinuationOptions.OnlyOnFaulted);
  
  Task.Factory.StartNew(() =&gt; DoSomething())
      .ContinueWith(t=&gt;Log.Error(t,"Error"),TaskContinuationOptions.OnlyOnFaulted);
  </code></pre>
  
  <p>After a successful run of <code>DoSomething</code>, <code>Task.Run</code> throws <code>TaskCanceledException</code> while <code>Task.Factory.StartNew</code> works fine. Why?</p>
  
  <p>further reading:
  <a href="http://blog.stephencleary.com/2013/08/startnew-is-dangerous.html" rel="nofollow noreferrer" title="Stephen Cleary">Stephen Clearly on why not use Task.Factory.StartNew</a><br/>
  <a href="https://blogs.msdn.microsoft.com/pfxteam/2011/10/24/task-run-vs-task-factory-startnew/" rel="nofollow noreferrer">MSDN Link</a></p>
  
  <p><b>UPDATE 2: </b>
  Sample Code:</p>
  
  <pre><code>private async void button27_Click(object sender, EventArgs e)
  {
      var r = new Random(System.DateTime.Now.Millisecond);
  
      await Task.Factory.StartNew(
          () =&gt; {
              Divide(r.Next(100), r.Next(-1, 10));
              Log.Information("Divide Done!");
          },
          CancellationToken.None,
          TaskCreationOptions.DenyChildAttach,
          TaskScheduler.Default)
      .ContinueWith(
          t =&gt; {
              Log.Error(t.Exception,"There is an exception on Divide");
          },
          TaskContinuationOptions.OnlyOnFaulted);
  }
  
  private static void Divide(int a, int b)
  {
      var c = a/b;
  }
  </code></pre>