I am trying to optimize this code to decrease the time taken to complete the for
loop. In this case, CreateNotification()
takes a long time and using async
await
does not improve performance as each asynchronous call is being awaited. I would like to use Task.WhenAll()
to optimize the code. How can I do this?
foreach (var notification in notificationsInput.Notifications)
{
try
{
var result = await CreateNotification(notification);
notification.Result = result;
}
catch (Exception exception)
{
notification.Result = null;
}
notifications.Add(notification);
}