Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 向列表中添加元素时异步并等待<;T>;_C#_Asynchronous_Multitasking_Async Await - Fatal编程技术网

C# 向列表中添加元素时异步并等待<;T>;

C# 向列表中添加元素时异步并等待<;T>;,c#,asynchronous,multitasking,async-await,C#,Asynchronous,Multitasking,Async Await,我编写了一个方法,它将来自许多源的元素添加到列表中。见下文: public static async Task<List<SearchingItem>> GetItemsToSelect() { List<SearchingItem> searchingItems = new List<SearchingItem>(); foreach (Place place in await GetPlaces())

我编写了一个方法,它将来自许多源的元素添加到列表中。见下文:

public static async Task<List<SearchingItem>> GetItemsToSelect()
    {
        List<SearchingItem> searchingItems = new List<SearchingItem>();

        foreach (Place place in await GetPlaces())
        {
            searchingItems.Add(new SearchingItem() { 
                IdFromRealModel=place.Id, NameToDisplay=place.FullName, 
                ExtraInformation=place.Name, TypeOfSearchingItem=TypeOfSearchingItem.PLACE });
        }

        foreach (Group group in await GetGroups())
        {
            searchingItems.Add(new SearchingItem()
            {
                IdFromRealModel = group.Id, NameToDisplay = group.Name,
                ExtraInformation = group.TypeName, TypeOfSearchingItem = TypeOfSearchingItem.GROUP
            });
        }

        return searchingItems;
    }
公共静态异步任务GetItemsToSelect()
{
列表搜索项=新列表();
foreach(将位置放入wait GetPlaces())
{
添加(新的SearchingItem(){
IdFromRealModel=place.Id,NameToDisplay=place.FullName,
ExtraInformation=place.Name,TypeOfSearchingItem=TypeOfSearchingItem.place});
}
foreach(等待GetGroups()中的组)
{
添加(新的SearchingItem()
{
IdFromRealModel=group.Id,NameToDisplay=group.Name,
ExtraInformation=group.TypeName,TypeOfSearchingItem=TypeOfSearchingItem.group
});
}
返回搜索项目;
}
我测试了这个方法,效果很好。我认为它可以正常工作,因为GetPlaces方法返回160个元素,GetGroups返回3000个。但是,我想知道如果这些方法同时返回元素,它是否会起作用。我应该锁定列表搜索项吗


谢谢你的建议

您的项目不会同时运行,请启动
GetPlaces()
,停止并等待
GetPlaces()
结果,然后进入第一个循环。然后启动
GetGroups()
,停止并等待
GetGroups()
结果,然后进入第二个循环。您的循环不是并发的,因此在添加它们时无需锁定

但是,如果您注意到两个异步方法也不是并发的,那么您可以很容易地修改您的程序使其成为并发的

public static async Task<List<SearchingItem>> GetItemsToSelect()
{
    List<SearchingItem> searchingItems = new List<SearchingItem>();
    var getPlacesTask = GetPlaces();
    var getGroupsTask = GetGroups();

    foreach (Place place in await getPlacesTask)
    {
        searchingItems.Add(new SearchingItem() { 
            IdFromRealModel=place.Id, NameToDisplay=place.FullName, 
            ExtraInformation=place.Name, TypeOfSearchingItem=TypeOfSearchingItem.PLACE });
    }

    foreach (Group group in await getGroupsTask)
    {
        searchingItems.Add(new SearchingItem()
        {
            IdFromRealModel = group.Id, NameToDisplay = group.Name,
            ExtraInformation = group.TypeName, TypeOfSearchingItem = TypeOfSearchingItem.GROUP
        });
    }

    return searchingItems;
}

我是否应该锁定列表搜索项
,否,因为在第一个循环完成之前,不会执行第二个循环。
public static async Task<List<SearchingItem>> GetItemsToSelect()
{
    var getPlacesTask = GetPlaces();
    var getGroupsTask = GetGroups();

    var places = await getPlacesTask;

    //Just make the initial list from the LINQ object.
    List<SearchingItem> searchingItems = places.AsParallel().Select(place=>
        new SearchingItem() { 
            IdFromRealModel=place.Id, NameToDisplay=place.FullName, 
            ExtraInformation=place.Name, TypeOfSearchingItem=TypeOfSearchingItem.PLACE 
        }).ToList();

    var groups = await getGroupsTask;

    //build up a PLINQ IEnumerable
    var groupSearchItems = groups.AsParallel().Select(group=>
        new SearchingItem()
        {
            IdFromRealModel = group.Id, NameToDisplay = group.Name,
            ExtraInformation = group.TypeName, TypeOfSearchingItem = TypeOfSearchingItem.GROUP
        });

    //The building of the IEnumerable was parallel but the adding is serial.
    searchingItems.AddRange(groupSearchItems);

    return searchingItems;
}