Working with multiple background tasks
In this section, we will see code samples for loading data from multiple sources in parallel, not waiting until the method is ready to return the data to the caller. The technique is slightly different for synchronous and asynchronous code, but the general idea is the same.
First, review this method that calls three async methods and uses Task.WhenAll
to wait before returning the patient data:
public async Task<Patient> LoadPatientAsync(int patientId)
{
var taskList = new List<Task>
{
LoadPatientInfoAsync(patientId),
LoadProviderAsync(patientId),
LoadMedicationsAsync(patientId)
};
await Task.WhenAll(taskList.ToArray());
_patient.Medications...