I am having trouble trying to correctly design the most efficient way to repeat several asynchronous tasks launched from the request object, and then perform some other async tasks that depend on both the request object and the result of the first async task. I am running the C # lambda function in AWS. I tried this model (error handling is omitted for brevity):
public async Task MyAsyncWrapper()
{
List<Task> Tasks = new List<Task>();
foreach (var Request in Requests)
{
var Continuation = this.ExecuteAsync(Request).ContinueWith(async x => {
var KeyValuePair<bool, string> Result = x.Result;
if (Result.Key == true)
{
await this.DoSomethingElseAsync(Request.Id, Request.Name, Result.Value);
Console.WriteLine("COMPLETED");
}
}
Tasks.Add(Continuation);
}
Task.WaitAll(Tasks.ToArray());
}
This approach makes the method DoSomethingElseAsync()
not really come to life, and in many of my calls to Lambda Function, I never get the output "COMPLETED". I also approached this method:
public async Task MyAsyncWrapper()
{
foreach (var Request in Requests)
{
KeyValuePair<bool, string> Result = await this.ExecuteAsync(Request);
if (Result.Key == true)
{
await this.DoSomethingElseAsync(Request.Id, Request.Name, Result.Value);
Console.WriteLine("COMPLETED");
}
}
}
, , , asnyc. Interleaved Tasks, , , , - , ', Request
. , :
List<Task<KeyValuePair<bool, string>>> Tasks = new List<Task<KeyValuePair<bool, string>>>();
foreach (var Request in Requests)
{
Tasks.Add(ths.ExecuteAsync(Request);
}
foreach (Task<KeyValuePair<bool, string>> ResultTask in Tasks.Interleaved())
{
KeyValuePair<bool, string> Result = ResultTask.Result;
await this.DoSomethingElseAsync(???, ???, Result.Value);
}
foreach? , ExecuteAsync()
, , .