How can I use the async keywords in a project targeting.net 4.0

You want the Microsoft.Bcl.Async package. That’s a properly released, non-CTP package that is stable. This also requires VS2012 since an updated compiler is needed to understand async and await. In theory one could use older tools from VS2010 along with the older CTP library, I’d strongly recommend that you don’t – it has bugs and … Read more

Channels with CancellationTokenSource with timeout memory leak after dispose

I was able to reproduce the issue you are observing. It is clearly a flaw in the Channels library IMHO. Here is my repro: using System; using System.Diagnostics; using System.Threading; using System.Threading.Channels; using System.Threading.Tasks; using System.Threading.Tasks.Dataflow; public static class Program { public static async Task Main() { var channel = Channel.CreateUnbounded<int>(); var bufferBlock = new … Read more

How to implement an efficient WhenEach that streams an IAsyncEnumerable of task results?

By using code from this article, you can implement the following: public static Task<Task<T>>[] Interleaved<T>(IEnumerable<Task<T>> tasks) { var inputTasks = tasks.ToList(); var buckets = new TaskCompletionSource<Task<T>>[inputTasks.Count]; var results = new Task<Task<T>>[buckets.Length]; for (int i = 0; i < buckets.Length; i++) { buckets[i] = new TaskCompletionSource<Task<T>>(); results[i] = buckets[i].Task; } int nextTaskIndex = -1; Action<Task<T>> continuation … Read more