我正在尝试运行连接到远程站点(通过网络)并返回通用列表的多个功能。但是我想同时运行它们。
例如:
public static List<SearchResult> Search(string title)
{
//Initialize a new temp list to hold all search results
List<SearchResult> results = new List<SearchResult>();
//Loop all providers simultaneously
Parallel.ForEach(Providers, currentProvider =>
{
List<SearchResult> tmpResults = currentProvider.SearchTitle((title));
//Add results from current provider
results.AddRange(tmpResults);
});
//Return all combined results
return results;
}
正如我所看到的,可能同时发生多次插入“结果” ...这可能会使我的应用程序崩溃。
如何避免这种情况?