Task.Run vs. Task.Factory.StartNew (Part 2)


Task.Run vs. Task.Factory.StartNew (Part 2) This post continue the previous post about the differences between Task.Run vs. Task.Factory,StartNew. In the previous part I was speaking about the scheduling aspect, on this part I will spot the async await behavior.   Check the following code snippet and think what can go wrong: Code Snippet private async Task ExecuteAsync() {     var sw = Stopwatch.StartNew();     await Task.Factory.StartNew(async () =>         {             Console.WriteLine("Start section");             await Task.Delay(1000);             Console.WriteLine("End section");         });     Console.WriteLine(sw.Elapsed); }   What output will it produce? Will it be something like the following output: ...
one comment

Task.Run vs. Task.Factory.StartNew (Part 1)


Task.Run vs. Task.Factory.StartNew (Part 1) This post will focus on the key difference between Task.Run and Task.Factory.StartNew. For you can drill down into more details on this excellent post by Stephen Toub. In general you have to know that Task.Run is more restricted version of Task.Factory.StartNew. Task.Run sets default which fit for most common cases. Task.Factory.StartNew is there for advance scenarios.   in general Task.Run is equivalent to: Code Snippet Task.Factory.StartNew(someAction,                 cancellationToken,                 TaskCreationOptions.DenyChildAttach,                 TaskScheduler.Default);   Why should you care about it and how can it be used right?...
no comments