Skip to content

Instantly share code, notes, and snippets.

@DanielLarsenNZ
Last active October 12, 2016 21:10
Show Gist options
  • Save DanielLarsenNZ/c664f5d642e3fe3a15444e6e5b9dd3f0 to your computer and use it in GitHub Desktop.
Save DanielLarsenNZ/c664f5d642e3fe3a15444e6e5b9dd3f0 to your computer and use it in GitHub Desktop.
Task.Result property Waits and blocks
using System;
using System.Threading.Tasks;
namespace HelloAsync
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Main: line 10");
Console.WriteLine("Main: line 11. Result = " + DoSomethingAsync().Result);
Console.WriteLine("Main: line 12");
while( !Console.KeyAvailable)
{
System.Threading.Thread.Sleep(1);
}
}
static private async Task<bool> DoSomethingAsync()
{
Console.WriteLine("DoSomethingAsync line 22");
System.Threading.Thread.Sleep(1000);
Console.WriteLine("DoSomethingAsync line 24");
return true;
}
}
}
/*
Output:
============================
Main: line 10
DoSomethingAsync line 22
DoSomethingAsync line 24
Main: line 11. Result = True
Main: line 12
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment