Created
September 9, 2016 09:42
-
-
Save mastoj/8dba36e17f8df4e73b991b15eaafdb19 to your computer and use it in GitHub Desktop.
Show the difference between multiple await and Task.WhenAll
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.CodeDom; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace AsyncDiff | |
{ | |
public class SomeViewModel | |
{ | |
public string Data { get; set; } | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var now = DateTime.Now; | |
Test(MultipleAwait); | |
Test(TaskWhenAll); | |
Console.ReadLine(); | |
} | |
private static void Test(Func<Task<SomeViewModel>> taskFunc) | |
{ | |
var now = DateTime.Now; | |
var x = taskFunc().Result; | |
var endTime = DateTime.Now; | |
Console.WriteLine($"{x.Data}: {(endTime - now)}"); | |
} | |
public static async Task<SomeViewModel> MultipleAwait() | |
{ | |
await SomeTask(); | |
await SomeTask(); | |
await SomeTask(); | |
return new SomeViewModel() { Data = "MultipleAwait" }; | |
} | |
public static async Task<SomeViewModel> TaskWhenAll() | |
{ | |
await Task.WhenAll(new[] {SomeTask(), SomeTask(), SomeTask()}); | |
return new SomeViewModel() { Data = "TaskWhenAll" }; | |
} | |
public static async Task SomeTask() | |
{ | |
await Task.Delay(2000); | |
// return Task.FromResult<object>(null); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment