Vai al contenuto

Speed up loop with ForEachAsync

I’m reposting an useful old post by Stephen Toub.

ForEach loop doesn’t supports Async/Await operators. If you like the possibility to have a foreach loop that can run in parallel, and can also leverage async/await operators to improve the performances of your loop, you can use the following, very useful, extension method.

public static class Extensions 
    public static Task ForEachAsync<TSource, TResult>(
        this IEnumerable<TSource> source,
        Func<TSource, Task<TResult>> taskSelector, Action<TSource, TResult> resultProcessor)
        var oneAtATime = new SemaphoreSlim(initialCount:1, maxCount:1);
        return Task.WhenAll(
            from item in source
            select ProcessAsync(item, taskSelector, resultProcessor, oneAtATime));

    private static async Task ProcessAsync<TSource, TResult>(
        TSource item,
        Func<TSource, Task<TResult>> taskSelector, Action<TSource, TResult> resultProcessor,
        SemaphoreSlim oneAtATime)
        TResult result = await taskSelector(item);
        await oneAtATime.WaitAsync();
        try { resultProcessor(item, result); }
        finally { oneAtATime.Release(); }


var collection = new List<object>();

await collection.ForEachAsync(
    async obj => { ... loop code, containing the await operators and a return statement ... },
    (obj, result) => { ...action to perform with the result of the operation... });


var client = new MyAsyncServiceClient();
var requestUrls = new List<string>();
var responseList = new List<Response>();

await requestUrls.ForEachAsync(
    async requestUrl => {
        var response = await client.ExecuteRequest(requestUrl);
        return response;
    (requestUrl, response) => { 

You can find here the original article, and here an useful post that explains the reasons behind the architectural choices.

Thanks Stephen!

D365 welcomes PowerApps Monitor

Performance issues? WebResources or PCFs not showing correctly? Odd script errors? PowerApps Monitor to the rescue!

Microsoft officially announced the availability of PowerApps Monitor for Modern Driven Apps (and D365 too).

The Monitor tool provides a way to view a stream of events from a user’s session in order to diagnose an issue. As of this blog post, it supports the following events on the latest build:

  • KPI for page loads, command executions, and other major events
  • Network request details
  • Warnings for synchronous XHR’s
  • Custom script errors (e.g. onload, ribbon rule evaluation)
  • Form execution event details (e.g. onload, onchange)
  • Form visibility reasons for controls and related menu items
  • Power BI control failure and performance events

Just add “&monitor=true” at the end of your page URL, and you’ll see the monitor icon on the top bar:

Original post: