I have a series of async methods that I would like to execute simultaneously. Each of these methods return true or false in regards to if they execute successfully or not. Their results are also logged in our audit trail so that we can diagnose issues.
Some of my functions are not dependent on all of these methods executing successfully, and we fully expect some of them to fail from time to time. If they do fail, the program will continue to execute and it will merely alert our support staff that they need to correct the issue.
I'm trying to figure out what would be the best method for all of these functions to execute simultaneously, and yet have the parent function await them only after they have all begun to execute. The parent function will return False if ANY of the functions fail, and this will alert my application to cease execution.
My idea was to do something similar to:
public async Task<bool> SetupAccessControl(int objectTypeId, int objectId, int? organizationId) { using (var context = new SupportContext(CustomerId)) { if (organizationId == null) { var defaultOrganization = context.Organizations.FirstOrDefault(n => n.Default); if (defaultOrganization != null) organizationId = defaultOrganization.Id; } } var acLink = AcLinkObjectToOrganiation(organizationId??0,objectTypeId,objectId); var eAdmin = GrantRoleAccessToObject("eAdmin", objectTypeId, objectId); var defaultRole = GrantRoleAccessToObject("Default", objectTypeId, objectId); await acLink; await eAdmin; await defaultRole; var userAccess = (objectTypeId != (int)ObjectType.User) || await SetupUserAccessControl(objectId); return acLink.Result && eAdmin.Result && defaultRole.Result && userAccess; } public async Task<bool> SetupUserAccessControl(int objectId) { var everyoneRole = AddToUserRoleBridge("Everyone", objectId); var defaultRole = AddToUserRoleBridge("Default", objectId); await everyoneRole; await defaultRole; return everyoneRole.Result && defaultRole.Result; } Is there a better option? Should I restructure in any way? I'm simply trying to speed up execution time as I have a parent function that executes close to 20 other functions that are all independent of each other. Even at it's slowest, without async, it only takes about 1-2 seconds to execute. However, this will be scaled out to eventually have that parent call executed several hundred times (bulk insertions).
Task.WhenAll. For the result values, it's hard to tell without knowing half of the types involved...