using System.Collections.Concurrent;
using System.Threading.Tasks;
public static void Main()
var strings = new[] { "1", "1", "1", "2", "2", "3" };
var tasks = strings.Select(test.ProcessSomeThing);
Task.WhenAll(tasks).Wait();
foreach (var task in tasks)
Console.WriteLine(task.Result);
Console.WriteLine("Hello World");
private ConcurrentDictionary<string, Lazy<Task<string>>> dict =
new ConcurrentDictionary<string, Lazy<Task<string>>>();
public Task<string> ProcessSomeThing(string input)
key => new Lazy<Task<string>>(() => GetSomeValueFromAsyncMethod(key), LazyThreadSafetyMode.ExecutionAndPublication),
(key, currentValue) => new Lazy<Task<string>>(() => GetSomeValueFromAsyncMethod(key), LazyThreadSafetyMode.ExecutionAndPublication)).Value;
private async Task<string> GetSomeValueFromAsyncMethod(string input)
throw new Exception("Every other time we throw...");
return "A value number: " + (++ counter);
Console.WriteLine(ex.Message);
return "An error was actually handled above...";