有人能给我解释一下如何返回一个Task的结果吗? 我现在正在尝试做以下操作,但我的Tasks没有返回我期望的列表?这里有什么问题吗?
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.Result
不是数组。 - Hamlet HakobyanTask
没有Result
属性 - 看起来tasks
应该是类型为List<Task<List<string>>>
。 - Lee