我有以下代碼:使用任務(TPL)內的通用字典
Dictionary<string, WSResponse> responseDictionary = new Dictionary<string, WSResponse>();
List<Task> taskList = new List<Task>();
foreach (string ID in IDs)
{
string localID = ID;
Task newTask = Task.Factory.StartNew(() =>
{
WSResponse response = Query.GetListFor(localID);
responseDictionary.Add(localID, response);
});
taskList.Add(newTask);
}
Task.WaitAll(taskList.ToArray());
我應該在這種情況下使用的ConcurrentDictionary
而不是Dictionary
?即使我確定密鑰不會在邏輯層面上重複?
很好的答案。謝謝! –