[英]c# await in for loop
如果缺少信息,我有一個需要更新的項目清單。 但是,我正在打電話給Google定位服務。 我想知道如何在可能的情況下異步添加必要的緯度和經度信息
我的代碼
public static void PullInfo()
{
foreach (var item in SAPItems)
{
if(item.MDM_Latitude == null || item.MDM_Longitude == null)
{
var point = GetMapPoint(item.AddressLine1 + " " + item.FiveDigitZip);
item.MDM_Latitude = point.Result.Latitude.ToString();
item.MDM_Longitude = point.Result.Longitude.ToString();
}
}
foreach(var item in SAPItems)
Console.WriteLine(item.MDM_Latitude + " " + item.MDM_Longitude);
}
private static async Task<MapPoint> GetMapPoint(string add)
{
var task = Task.Run(() => LocationService.GetLatLongFromAddress(add));
return await task;
}
您可以通過多個任務異步獲取多個地圖點(注意,它需要將PullInfo()
轉換為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));
}
如果PullInfo()
無法轉換為async-await,則可以強制線程等待結果,但是它將阻塞當前線程:
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));
}
最后一個代碼示例的運行示例: https : //ideone.com/0uXGlG
您只需要等待獲取數據的調用即可(注意await
是如何從GetMapPoint
):
public static async Task PullInfo()
{
foreach (var item in SAPItems)
{
if(item.Latitude == null || item.Longitude == null)
{
var point = await GetMapPoint(item.AddressLine1 + " " + item.FiveDigitZip);
item.MDM_Latitude = point.Latitude.ToString();
item.MDM_Longitude = point.Longitude.ToString();
}
}
foreach(var item in SAPItems)
Console.WriteLine(item.MDM_Latitude + " " + item.MDM_Longitude);
}
private static Task<MapPoint> GetMapPoint(string add)
{
var task = Task.Run(() => LocationService.GetLatLongFromAddress(add));
return task;
}
您無需修改SAPItems
集合,而只需修改每個單獨的項目。 獲得響應后,您只需更新循環中的當前項目即可。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.