static void Main(string[] args)
 {
     int[] ids = Enumerable.Range(1, 100).ToArray();
     Parallel.ForEach(ids, id => {
         var processor = new SomeTaskProcessor(id);
         processor.ExecuteLongRunningProcessThatReadsDbAndCreatesSomeNewRecords();
     });
 }
    static void Main(string[] args)
    {
        int[] ids       = Enumerable.Range(1, 100).ToArray();;
        var   stopwatch = new Stopwatch();

        stopwatch.Start();
        Parallel.ForEach(ids, id => {
            var processor = new SomeTaskProcessor(id);
            processor.ExecuteLongRunningProcessThatReadsDbAndCreatesSomeNewRecords();
        });
        // ~23 seconds
        stopwatch.Stop();
        Console.WriteLine(stopwatch.Elapsed.ToString());
    }
Exemplo n.º 3
0
    static async Task Main(string[] args)
    {
        int[] ids       = Enumerable.Range(1, 100).ToArray();;
        var   stopwatch = new Stopwatch();

        stopwatch.Start();
        var tasks = ids.Select(id => {
            var processor = new SomeTaskProcessor(id);
            return(processor.ExecuteLongRunningProcessThatReadsDbAndCreatesSomeNewRecords());
        }).ToArray();
        await Task.WhenAll(tasks);

        // ~8 seconds
        stopwatch.Stop();
        Console.WriteLine(stopwatch.Elapsed.ToString());
    }