Skip to content

Instantly share code, notes, and snippets.

@clivefoley
Created January 22, 2019 11:19
Show Gist options
  • Save clivefoley/57640e02bff1f0c9766b65ca903cdc3c to your computer and use it in GitHub Desktop.
Save clivefoley/57640e02bff1f0c9766b65ca903cdc3c to your computer and use it in GitHub Desktop.
An example of using tasks and continueWith
using System;
using System.Threading.Tasks;
namespace TasksExample
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Welcome to the Tasks example app.");
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("I'm going to run a number of tasks. Press any key to start...");
Console.ReadLine();
Console.WriteLine("Im creating a task which is 'completed' by default");
Task<string> t = Task.FromResult("I'm the result");
Console.WriteLine("Task details: " + t.Status + " '" + t.Result + "'");
Console.WriteLine();
Console.WriteLine("Ready for the next one?");
Console.ReadLine();
Console.WriteLine("Ok lets chain tasks together....");
Task chain = stepOne().ContinueWith(fs => stepTwo()).ContinueWith(s => stepThree(s.Id)).ContinueWith(s => finalStep(DateTime.Now));
chain.Wait();
Console.WriteLine("The chain finished!");
Console.WriteLine("Chain details: " + chain.Status);
}
static Task stepOne()
{
Console.WriteLine("I'm the first step.");
return Task.CompletedTask;
}
static Task stepTwo() {
Console.WriteLine("I'm the second step.");
return Task.CompletedTask;
}
static Task stepThree(int previousId) {
Console.WriteLine("I'm the third step. " + previousId );
return Task.CompletedTask;
}
static Task finalStep(DateTime time)
{
Console.WriteLine("I'm the final step. " + time);
return Task.CompletedTask;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment