CSharp · Console
using System;
using System.Threading.Tasks;
using System.Threading;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
Console.WriteLine(SynchronizationContext.Current == null);
new Program().DoSomething().Wait();
}
public async Task DoSomething()
{
Console.WriteLine("Starting thread: " + Thread.CurrentThread.ManagedThreadId);
var listofProcessor = new List<Processor>{new Processor("Processor"), new Processor("Processor1")};
try
{
var tasks = listofProcessor.Select(x => x.Process());
Console.WriteLine("Before Task.WhenAll " + Thread.CurrentThread.ManagedThreadId);
await Task.WhenAll(tasks);
Console.WriteLine("After Task.WhenAll " + Thread.CurrentThread.ManagedThreadId);
}
catch (Exception e)
{
Console.WriteLine("Exception caught");
}
}
public class Processor
{
private string _name;
public Processor(string name)
{
_name = name;
}
public async Task<string> Process()
{
Console.WriteLine(_name + " BeforeAwait " + Thread.CurrentThread.ManagedThreadId);
await Task.Delay(100);
Console.WriteLine(_name + " AfterAwait " + Thread.CurrentThread.ManagedThreadId);
return "hellow";
}
}
}