-
-
Save shammelburg/cf93e102d6252c56e3bc8a4b531dbe9a to your computer and use it in GitHub Desktop.
Bundle multiple asynchronous calls which helps with performance.
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
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