Skip to content

Instantly share code, notes, and snippets.

@ElemarJR
Created August 1, 2012 13:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ElemarJR/3226841 to your computer and use it in GitHub Desktop.
Save ElemarJR/3226841 to your computer and use it in GitHub Desktop.
namespace AsyncDemo
{
class Program
{
static void Main(string[] args)
{
new Program().Run();
Console.WriteLine("Press any key to exit...");
Console.ReadLine();
}
public async void Run()
{
await Process.Start("Notepad.exe");
Console.WriteLine("Notepad was finished");
}
}
public static class AwaiterExtensions
{
public static TaskAwaiter<int> GetAwaiter(this Process that)
{
var tcs = new TaskCompletionSource<int>();
that.EnableRaisingEvents = true;
that.Exited += (s, e) => tcs.TrySetResult(that.ExitCode);
if (that.HasExited) tcs.TrySetResult(that.ExitCode);
return tcs.Task.GetAwaiter();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment