Skip to content

Instantly share code, notes, and snippets.

@mahldcat
Last active July 29, 2021 15:52
Show Gist options
  • Select an option

  • Save mahldcat/ab471cd1bf99e6c5461f4800f0db8e3c to your computer and use it in GitHub Desktop.

Select an option

Save mahldcat/ab471cd1bf99e6c5461f4800f0db8e3c to your computer and use it in GitHub Desktop.
Skeletal example of using the async/await pattern.....
[HttpPost("urlParam")]
public async Task<MyResponse> PostAsync(string urlParam, [FromQuery] string arg2, [FromBody] MyBody parsableBodyData)
{
MyResponse mr = new MyResponse();
List<Task> tasks = new List<Task>();
foreach(IWorkerTask w in workList){
tasks.Add(w.DoWorkAsync(...).ContinueWith((task)=>{
//handle post processing from task....
//task.Result will contain the return value of the async
mr.Result.Add(taskResult);
}));
}
Task.AwaitAll(tasks.ToArray());
return mr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment