I wrote method, which adds elements to the List from many sources. See below:
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;
}
I tested this method and works propertly. I suppose that it works propertly, because GetPlaces method return 160 elements and GetGroups return 3000. But, I was wondering if it will work if the methods return elements in the same time. Should I lock list searchingItems ?
Thank you for advice.
Your items do not run at the same time, you start
GetPlaces()
, stop and wait forGetPlaces()
result, then go in to the first loop. You then startGetGroups()
, stop and wait forGetGroups()
result, then go in to the second loop. Your loops are not concurrent so you have no need to lock while adding them.However if you have noticed your two async methods are also not concurrent, you can easily modify your program to make it so though.
What this will do will start
GetPlaces()
, startGetGroups()
, stop and wait forGetPlaces()
result, then go in to the first loop, stop and wait forGetGroups()
result, then go in to the second loop.The two loops are still not concurrent, but your two await-able methods are which may give you a small performance boost. I doubt you would get any benifit from making the loops concurrent, they appear to just be building models and the overhead of making it thread safe would not be worth it for how little work is being done.
If you really wanted to try and make it more parallel (but I doubt you will see much benefit) is use PLINQ to build your models.