C#在for循环中等待

时间:2018-08-29 16:57:36

标签: c# async-await

我有一份清单,如果缺少信息,则需要更新。但是,我正在打电话给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;
}

2 个答案:

答案 0 :(得分:4)

您可以通过多个任务异步获取多个地图点(注意,它需要将PullInfo()转换为异步等待):

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()无法转换为异步等待,则可以强制线程等待结果,但是它将阻塞当前线程:

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

答案 1 :(得分:0)

您只需要等待调用以获取数据(注意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集合,而只需修改每个单独的项目。得到响应后,就可以更新循环中的当前项目。