Tuesday, October 11, 2016

Multiple threads with await operator and Task

class Program
{
static void Main(string[] args)
{
StartTaskThreads();
Console.ReadKey();
}

static private async void StartTaskThreads()
{
const int numThreads = 5;
List<Task> tasks = new List<Task>();
for (int i=0; i< numThreads; i++)
tasks.Add(InfiniteTask());

await Task.WhenAll(tasks);
}

static private async Task InfiniteTask()
{
while (true)
{
Console.WriteLine(Thread.CurrentThread.ManagedThreadId);
await Task.Delay(500);
}
}
}