Skip to content

Instantly share code, notes, and snippets.

@jskeet
Created November 24, 2017 18:19
Show Gist options
  • Save jskeet/27e26a97907af2488f0d7d5b0dbe06a8 to your computer and use it in GitHub Desktop.
Save jskeet/27e26a97907af2488f0d7d5b0dbe06a8 to your computer and use it in GitHub Desktop.
using System;
using System.Threading;
using System.Threading.Tasks;
public class Test
{
static void Main()
{
Task task = DelayAndPrintAsync();
Console.WriteLine("Task returned to Main. Sleeping for 10s");
Thread.Sleep(10000);
Console.WriteLine("Waiting for task to finish");
// Normally you really don't want to use Task.Wait(), but
// in a console app's main thread it's okay.
task.Wait();
}
static async Task DelayAndPrintAsync()
{
Console.WriteLine("In DelayAndPrintAsync before first delay");
await Task.Delay(5000);
Console.WriteLine("In DelayAndPrintAsync between delays");
await Task.Delay(10000);
Console.WriteLine("In DelayAndPrintAsync after second delay");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment