Code after Task.Delay in delegate not executing

by Forrest Kribs   Last Updated September 11, 2019 17:26 PM

I am trying to concurrently process partitions of a list. For each integer in each partition of the list I am trying to wait for that amount of time.

public class Program
{
    public async static void Main()
    {
        Console.WriteLine("Starting.");
        var values = Enumerable.Range(0,1000).ToList();

        await Task.WhenAll(
            from partition in Partitioner.Create(values).GetPartitions(10)
            select Task.Run(async delegate {
                Console.WriteLine("Entered");
                using (partition)
                    while (partition.MoveNext()){               
                        var delay = partition.Current;
                        await Task.Delay(delay);
                        Console.WriteLine(string.Format("Waited {0} milliseconds",delay));

                    }
            }));
        Console.WriteLine("Done");
    }
}

Execution appears to stop after Task.Delay(delay):

Starting.
Entered
Entered
Waited 0 milliseconds
Entered
Entered
Entered
Entered
Entered
Entered
Entered
Entered


Related Questions


Updated June 12, 2015 16:11 PM

Updated August 06, 2015 18:11 PM

Updated August 19, 2016 08:12 AM

Updated February 17, 2017 21:26 PM

Updated March 13, 2017 05:26 AM