Can someone explain to me how to return the result of a Task? I currently am trying to do the following but my Tasks are not returning my List that I expect? What is the problem here?
static void Main() { List<Task> tasks = new List<Task>(); List<string> sha256_hashes = new List<string>(); List<string> results = new List<string>(); sha256_hashes.Add("hash00"); sha256_hashes.Add("hash01"); sha256_hashes.Add("hash03"); foreach(string sha256 in sha256_hashes) { string _sha256 = sha256; var task = Task.Factory.StartNew(() => GetAdditionalInfo(_sha256)); tasks.Add(task); } Task.WaitAll(tasks.ToArray()); //I want to put all the results of each task from tasks but the problem is //I can't use the Result method from the _task because Result method is not available //below is my plan to get all the results: foreach(var _task in tasks) { if(_task.Result.Count >= 1) //got an error Only assignment, call, increment, dec.... results.AddRange(_task.Result); //got an error Only assignment, call, increment, dec.... } //Do some work about results } static List<string> GetAdditionalInfo(string hash) { //this code returns information about the hash in List of strings }
Task.Resultisn't array.Taskdoesn't have aResultproperty - it looks liketasksshould have typeList<Task<List<string>>>