using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks;
public static void Main()
int number = Process.GetCurrentProcess().Threads.Count;
Console.WriteLine("Init thread count:" + number);
Task.WaitAll(unwrapped());
number = Process.GetCurrentProcess().Threads.Count;
Console.WriteLine("Done thread count:" + number);
private static async Task wrapped()
Console.WriteLine("wrapped");
List<Task> tasks = new List<Task>();
Task.Run(async() => await new MyThread().RunMe()),
Task.Run(async() => await new MyThread().RunMe()),
Task.Run(async() => await new MyThread().RunMe()),
Task.Run(async() => await new MyThread().RunMe()),
int number = Process.GetCurrentProcess().Threads.Count;
Console.WriteLine("While running thread count:" + number);
await Task.WhenAll(tasks);
private static async Task unwrapped()
Console.WriteLine("unwrapped");
List<Task> tasks = new List<Task>();
Task<int> t1 = new MyThread().RunMe();
Task<int> t2 = new MyThread().RunMe();
Task<int> t3 = new MyThread().RunMe();
Task<int> t4 = new MyThread().RunMe();
tasks.AddRange(new[] {t1, t2, t3, t4});
int number = Process.GetCurrentProcess().Threads.Count;
Console.WriteLine("While running thread count:" + number);
public static int _counter;
public async Task<int> RunMe()
for (int i = 0; i < 2; ++i)
Console.WriteLine("T" + Thread.CurrentThread.ManagedThreadId + " " + i);
Console.WriteLine("T" + Thread.CurrentThread.ManagedThreadId + " done");