private static async Task <Response[]> ExecServicesAsync(ITAPServices svc, UserId uid, Request[] requests) { Task <Response>[] responseTasks = new Task <Response> [requests.Length]; Session session; try { session = await svc.LoginAsync(uid); } catch { //throw illegal user exception throw; } for (int i = 0; i < requests.Length; i++) { try { responseTasks[i] = svc.ExecServiceAsync(session, requests[i]); } catch { //on server too busy exception responseTasks[i] = null; } } await Task.WhenAll(responseTasks); await svc.LogoutAsync(session); return(responseTasks.Select(task => task.Result).ToArray()); }
/// <summary> /// Versão original (promove o bloqueio de threads), /// acabando por ser equivalente a um aversão síncrona /// </summary> /// <param name="svc"></param> /// <param name="uid"></param> /// <param name="requests"></param> /// <returns></returns> public static Task <Response[]> ExecServicesOldAsync(ITAPServices svc, UserID uid, Request[] requests) { return(Task.Run(() => { Session session = svc.LoginAsync(uid).Result; try { Response[] responses = new Response[requests.Length]; for (int i = 0; i < requests.Length; i++) { responses[i] = svc.ExecServiceAsync(session, requests[i]).Result; } return responses; } finally { try { svc.LogoutAsync(session).Wait(); } catch { } } })); }
/// <summary> /// Versão que optimiza o paralelismo potencial /// </summary> /// <param name="svc"></param> /// <param name="uid"></param> /// <param name="requests"></param> /// <returns></returns> public static async Task <Response[]> ExecServicesAsync(ITAPServices svc, UserID uid, Request[] requests) { Session session = await svc.LoginAsync(uid); try { Task <Response>[] tasks = new Task <Response> [requests.Length]; for (int i = 0; i < requests.Length; i++) { tasks[i] = svc.ExecServiceAsync(session, requests[i]); } return(await Task.WhenAll(tasks)); } finally { try { await svc.LogoutAsync(session); } catch { } } }