Praveen Kumar

Praveen Kumar

  • NA
  • 235
  • 20.4k

How to use Asynch instead of synchcronous

Jul 13 2020 1:18 AM
Hi,
 
How to use
  1. [HttpPost]  
  2. [ValidateAntiForgeryToken]  
  3. public ActionResult ReRunFailedSelectedBilling(List<BillingFailedViewModel> FailedGroups)  
  4. {  
  5. if (_logger.IsInfoEnabled)  
  6. _logger.LogInfo(string.Format("Entering ReRunFailedSelectedBilling"));  
  7. List<BillingFailedViewModel> CGs = FailedGroups.Where(x => x.ReRunFlag).ToList();  
  8. // Re-using code to get something working for GPSI. This should actually remove all the CGs and then process a single WFR. For now, we'll process one WFR for each CG...  
  9. foreach (BillingFailedViewModel group in CGs)  
  10. {  
  11. using (var dbContextScope = _dbContextScopeFactory.Create())  
  12. {  
  13. ReRunModel run = new ReRunModel((int)group.Id, DbConnection, ServerURL);  
  14. var login = HttpContext.User.Identity.Name;  
  15. run.Reject(login);  
  16. if (run._Status.Success == false)  
  17. {  
  18. TempData["Failure"] = "Error in rejection. Failed to reject.";  
  19. }  
  20. else  
  21. {  
  22. // Create a new workflow request and re-run  
  23. var wfr = run.SetWorkflowRequest();  
  24. dbContextScope.SaveChanges();  
  25. run.WorkflowRequestId = wfr.Id;  
  26. run.Run(run.RunSychronousService(), login); Question : How to use Asynch instead of synchcronous  
  27. }  
  28. }  
  29. }  
  30. }  
Asynch Method available in class - startrunmodel.cs
  1. public async Task RunAsync(string login)  
  2. {  
  3. logger.LogInfo(string.Format("PERF RunAsync starting"));  
  4. // Kick off the server job  
  5. string customer = wfRepo.GetCurrentUserCustomer(_User.WindowsUserName).Customer.Code;  
  6. ServiceStatus status = new ServiceStatus { Success = false, Message = "Run failed." };  
  7. Task[] wfrTasks = new Task[WorkflowRequests.Count()];  
  8. int idx = 0;  
  9. foreach (var wfr in WorkflowRequests)  
  10. {  
  11. WorkflowRequest request = new WorkflowRequest()  
  12. {  
  13. WorkflowRequestId = wfr.Id.ToString(),  
  14. CustomerCode = customer,  
  15. UserName = _User.WindowsUserName,  
  16. LoginName = login  
  17. };  
  18. try  
  19. {  
  20. logger.LogInfo(string.Format("PERF RequestBillingAsync {0} called", wfr.Id.ToString()));  
  21. wfrTasks[idx++] = _Service.RequestBillingAsync(request);  
  22. }  
  23. catch (Exception e)  
  24. {  
  25. logger.LogError("RequestBilling failed ", e);  
  26. }  
  27. logger.LogInfo(string.Format("PERF RequestBillingAsync {0} returned", wfr.Id.ToString()));  
  28. //_Status.Add(status);  
  29. }  
  30. //await Task.WhenAll(wfrTasks);  
  31. await Task.Yield();  
  32. logger.LogInfo(string.Format("PERF RunAsync ending "));  
  33. // return status;  
  34. }  

Answers (3)