@Pakojonez , there are multiple ways to achieve parallelism in .net with rich set of types and API in TPL. In your example of independent API calls where call to one API does not depend on the result of the other, you can do either of the below:
- Spawning multiple calls to API (task) but not awaiting until needed like below: // Note: .ConfigureAwait(false) slightly improves the performance https://devblogs.microsoft.com/dotnet/configureawait-faq/
var api1Task = CallApi1Async().ConfigureAwait(false); // Note I am not awaiting so that it does not block next until complete
var api2Task = CallApi2Async().ConfigureAwait(false); // Note I am not awaiting so that it does not block next until complete
var api3Task = CallApi3Async().ConfigureAwait(false); // Note I am not awaiting so that it does not block next until complete // now below I can do an await on each, this way, the overall time taken would be limited by the time taken by the longest running task and not the sum of them
var api1Result = await api1Task;
var api2Result = await api2Task;
var api3Result = await api3Task;
- Spawning multiple calls to API (task) and doing Task.WhenAll at the end like below: var tasks = new List<Task>();
tasks.Add(CallApi3Async()); var results = await Task.WhenAll(tasks).ConfigureAwait(false);
- For more sophisticated task scheduling, you can also consider using Dataflow blocks. This dataflow model promotes actor-based programming by providing in-process message passing for coarse-grained dataflow and pipelining tasks.
For general .net questions like this, I would suggest you to leverage asp.net forum or stackoverflow to get broader community reach on such general programming topics.