Skip to content

Instantly share code, notes, and snippets.

@davidfowl
Created February 8, 2017 04:02
Show Gist options
  • Save davidfowl/decbf2441ecf6a672538f66cd531b08b to your computer and use it in GitHub Desktop.
Save davidfowl/decbf2441ecf6a672538f66cd531b08b to your computer and use it in GitHub Desktop.
Async quiz: How many threads are used? What does it print?
using System;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
class Program
{
static void Main(string[] args)
{
var tcs = new TaskCompletionSource<object>();
var task = SomethingAsync(tcs);
Console.WriteLine("Hello");
tcs.TrySetResult(null);
task.Wait();
}
private static async Task SomethingAsync(TaskCompletionSource<object> tcs)
{
await tcs.Task;
Console.WriteLine("World");
}
}
@Tuatan
Copy link

Tuatan commented Mar 17, 2017

1 and "Hello World"

@Roman-Blinkov
Copy link

@Tuatan, try to add additional Console.Readline() methods after each line and check number of threads in Windows Task Manager
After this try same under linux :)
Sure things like http://mattwarren.org/2017/02/07/The-68-things-the-CLR-does-before-executing-a-single-line-of-your-code/ must be here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment