Created
April 14, 2016 13:35
-
-
Save greglecki/51c8ba7e54afb07964304af531d16ea1 to your computer and use it in GitHub Desktop.
Bundle multiple asynchronous calls which helps with performance.
This file contains 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
var testRepo = new TestRepo(); | |
//var start = DateTime.Now; | |
var t1 = testRepo.Task1(); | |
var t2 = testRepo.Task2(); | |
int[] r = await Task.WhenAll(t1, t2); | |
//var end = (DateTime.Now - start).Seconds; | |
return new int[] { r[0], r[1] }; | |
/***************************/ | |
public class TestRepo | |
{ | |
public Task<int> Task1() | |
{ | |
var t = Task.Run<int>(() => | |
{ | |
Thread.Sleep(2000); | |
return 20; | |
}); | |
return t; | |
} | |
public Task<int> Task2() | |
{ | |
var t = Task.Run<int>(() => | |
{ | |
Thread.Sleep(5000); | |
return 50; | |
}); | |
return t; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment