Last active
October 12, 2016 21:10
-
-
Save DanielLarsenNZ/c664f5d642e3fe3a15444e6e5b9dd3f0 to your computer and use it in GitHub Desktop.
Task.Result property Waits and blocks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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