Running Tasks In Parallel And Collecting Results In C# 11

Introduction

Today, we will see how to run multiple tasks in parallel and collect the results in C# 11. This technique can help us use the multi-processors we have on our hardware and improve the performance and efficiency of our applications.

Creating the project and code

I will be creating this application using Visual Studio 2022 Community edition. Please follow the below steps,

Running Tasks In Parallel And Collecting Results In C# 11

Running Tasks In Parallel And Collecting Results In C# 11

Running Tasks In Parallel And Collecting Results In C# 11

Running Tasks In Parallel And Collecting Results In C# 11

Once the project is created, add the below four classes:

namespace ParallelTasksAndResults {
    internal class ResultsData {
        public string ? SearchType {
            get;
            set;
        }
        public int NumberOfResults {
            get;
            set;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ParallelTasksAndResults {
    internal abstract class CollectData {
        public abstract Task < ResultsData > CollectDataAsync();
    }
}
namespace ParallelTasksAndResults {
    internal class CollectDataA: CollectData {
        public override async Task < ResultsData > CollectDataAsync() {
            await Task.Delay(1000);
            return new ResultsData {
                SearchType = "A",
                    NumberOfResults = 100
            };
        }
    }
}
namespace ParallelTasksAndResults {
    internal class CollectDataB: CollectData {
        public override async Task < ResultsData > CollectDataAsync() {
            await Task.Delay(1000);
            return new ResultsData {
                SearchType = "B",
                    NumberOfResults = 200
            };
        }
    }
}

The application now looks as below,

Running Tasks In Parallel And Collecting Results In C# 11

Finally, add the below code in the "Program.cs" file.

using ParallelTasksAndResults;
List<Task<ResultsData>> tasks = new List<Task<ResultsData>>();
tasks.Add(new CollectDataA().CollectDataAsync());
tasks.Add(new CollectDataB().CollectDataAsync());
ResultsData[] results = await Task.WhenAll(tasks);
foreach (ResultsData result in results)
{
    Console.WriteLine($"Search Type: {result.SearchType}, Number of Results: {result.NumberOfResults}");
}

The application is quite simple. We have a return type, "ResultsData". We create a list of tasks that return this type. We then add the two implementations of "CollectData" which are "CollectDataA" and "CollectDataB". Both these classes have a function that returns the "ResultsData" type. These are added to the list of Tasks. Then, we run the main command. This is "Task.WhenAll". This runs both functions in parallel and returns the results in a "ResultsData" array. We then display these results on the console.

Running Tasks In Parallel And Collecting Results In C# 11

The code is available at the below location:

https://github.com/munibrbutt/articles-code/tree/main/Run%20Parallel%20Tasks%20and%20Collect%20Results/ParallelTasksAndResultsSol

Running Tasks In Parallel And Collecting Results In C# 11

Summary

In today's article, we looked at how to run multiple tasks in parallel in C# 11. This technique allows us to utilize the multi-cores we have on our hardware, making our applications run faster and more responsive. The above example is quite simple, but the same logic can be applied for more complex scenarios.


Similar Articles