Last active
August 29, 2016 05:33
-
-
Save ericbrunner/47c740626bbbcbfba5fd4c068d897b47 to your computer and use it in GitHub Desktop.
Sample Async Await with ContinueWith
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
class Program | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
try | |
{ | |
Task<int> getNumberTask = GetNumberAsync(12); | |
Task continuationTask = getNumberTask.ContinueWith(async (numberTask) => | |
{ | |
Console.WriteLine("Continuation in progress..."); | |
int echo = numberTask.Result; | |
Console.WriteLine($"GetNumberAsync Echo: {echo}"); | |
await Task.Delay(2000).ConfigureAwait(false); | |
Console.WriteLine("Coninuation done."); | |
}); | |
Console.WriteLine(getNumberTask == continuationTask ? "getNumberTask == continuationTask" : "getNumberTask != continuationTask"); | |
Task.WaitAll(getNumberTask, continuationTask); | |
Console.WriteLine("Main done."); | |
} | |
catch (Exception e) | |
{ | |
Console.WriteLine(e); | |
} | |
Console.ReadKey(); | |
} | |
public static async Task<int> GetNumberAsync(int echo) | |
{ | |
await Task.Delay(2000).ConfigureAwait(false); | |
return echo; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment