Skip to content

Instantly share code, notes, and snippets.

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