Skip to content

Instantly share code, notes, and snippets.

@jpeckham
Created April 17, 2018 01:33
Show Gist options
  • Save jpeckham/1b3a0958d292a4b9bc5c3a73a48d19a7 to your computer and use it in GitHub Desktop.
Save jpeckham/1b3a0958d292a4b9bc5c3a73a48d19a7 to your computer and use it in GitHub Desktop.
TPL Task.Run example
using System;
using System.Threading;
using System.Threading.Tasks;
namespace AsyncAwaitStuff
{
class Program
{
static void Main(string[] args)
{
var result = Task.Run(DoSomeWork);
Console.WriteLine("Task Running");
Console.WriteLine(result.Result);//blocks until finished i think.
Console.WriteLine("Task Done Running");
Console.ReadKey();
}
private static async Task<string> DoSomeWork()
{
Console.WriteLine("Entering DoSomeWork()");
await Task.Run(() =>Thread.Sleep(5000));
Console.WriteLine("Leaving DoSomeWork()");
return "From DoSomeWork()";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment