The article highlights the importance of limiting the concurrent asynchronous operations, which in turn improves performance.
Consider an example where the user wants to load data asynchronously within a method, and it highlights the common mistake developers make.
(async item => await ProcessItem(item));
/// <summary>
/// Old approach with classic async await
/// </summary>
/// <returns></returns>
public async static Task OldApproach(List<string> items)
{
var tasks = items.Select(async item => await ProcessItem(item));
await Task.WhenAll(tasks);
}
The approach may look clean and simple, but it initiates tasks for each item in the collection concurrently. This can cause system strain under heavy List<string> items, which will produce poor application performance.
Let’s transform the above method using SemaphoreSlim to limit the number of concurrent asynchronous operations. The following code snippet demonstrates the more refined approach.
/// <summary>
/// Optimized approach with limit concurrency
/// </summary>
/// <returns></returns>
public static async Task OptimizedApproachAsync(List<string> items, int maxConcurrency = 10)
{
using (var semaphore = new SemaphoreSlim(maxConcurrency))
{
var tasks = items.Select(async item =>
{
await semaphore.WaitAsync(); // Limit concurrency by waiting for the semaphore.
try
{
await ProcessItem(item);
}
finally
{
semaphore.Release(); // Release the semaphore to allow other operations.
}
});
await Task.WhenAll(tasks);
}
}
The aforementioned code prevents the system from being choked by too many concurrent tasks.
Please find below the best practices
To balance system load and resources, it is recommended to use SemaphoreSlim
Avoid using .Result or .Wait(), as they can lead to deadlocks and degrade performance.
Avoid mixing async and sync code. Ensure all methods are async from top to bottom to prevent deadlock and make optimal use of resources.
Asynchronous programming in C# involves more than just understanding the async and await keywords; it requires more features like concurrency, resource utilization, and code structure
GitHub — ssukhpinder/30DayChallenge.Net
Thank you for being a part of the C# community! Before you leave:
Follow us: X | LinkedIn | Dev.to | Hashnode | Newsletter | Tumblr
Visit our other platforms: GitHub | Instagram | Tiktok | Quora | Daily.dev
More content at C# Programming
Also published here.