Skip to content

Instantly share code, notes, and snippets.

@ericbrunner
Last active August 29, 2016 05:33
Show Gist options
  • Save ericbrunner/47c740626bbbcbfba5fd4c068d897b47 to your computer and use it in GitHub Desktop.
Save ericbrunner/47c740626bbbcbfba5fd4c068d897b47 to your computer and use it in GitHub Desktop.
Sample Async Await with ContinueWith
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