You can get multiple map points asynchronously with multiple tasks (notice that it requires to convert PullInfo() to async-await):
public static async Task PullInfo() { // Create tasks to update items with latitude and longitude var tasks = SAPItems.Where(item => item.Latitude == null || item.Longitude == null) .Select(item => GetMapPoint(item.AddressLine1 + " " + item.FiveDigitZip) .ContinueWith(pointTask => { item.MDM_Latitude = pointTask.Result.Latitude.ToString(); item.MDM_Longitude = pointTask.Result.Longitude.ToString(); })); // Non-blocking await for tasks completion await Task.WhenAll(tasks); // Iterate to append Lat and Long foreach(var item in SAPItems) Console.WriteLine(item.MDM_Latitude + " " + item.MDM_Longitude); } private static Task<MapPoint> GetMapPoint(string add) { return Task.Run(() => LocationService.GetLatLongFromAddress(add)); }
If PullInfo() cannot be converted to async-await, you can force the thread to wait for the results, but it will block the current thread:
public static void PullInfo() { // Create tasks to update items with latitude and longitude var tasks = SAPItems.Where(item => item.Latitude == null || item.Longitude == null) .Select(item => GetMapPoint(item.AddressLine1 + " " + item.FiveDigitZip) .ContinueWith(pointTask => { item.MDM_Latitude = pointTask.Result.Latitude.ToString(); item.MDM_Longitude = pointTask.Result.Longitude.ToString(); })); // Wait for tasks completion (it will block the current thread) Task.WaitAll(tasks.ToArray()); // Iterate to append Lat and Long foreach(var item in SAPItems) Console.WriteLine(item.MDM_Latitude + " " + item.MDM_Longitude); } private static Task<MapPoint> GetMapPoint(string add) { return Task.Run(() => LocationService.GetLatLongFromAddress(add)); }
Running example of this last code sample: https://ideone.com/0uXGlG
.Resultwhen you await yourGetMapPointfunction? After the await, you already have the data, just await there, not insideGetMapPoint.var point = await GetMapPoint(item.AddressLine1 + " " + item.FiveDigitZip);