[英]Troubles with Task getting Task result (cannot implicitly convert System.Threading.Task.Task to string[])
Task t = new Task(() => grabber.grab(link));
var x = Task.WhenAny(t, Task.Delay(TimeSpan.FromSeconds(3)));
if (x.Result != null)
{
// error cannot implicitly convert System.Threading.Task.Task to string[]
string[] result = x.Result;
foreach (string item in result)
{
list.Add(item);
}
}
The function grab is totally synchronous and returns a string array 函数抓取是完全同步的,并返回一个字符串数组
You're checking the result from the x
Task, which is the Task that can be either the same as t
or the result of Task.Delay(TimeSpan.FromSeconds(3))
(provided you await
it). 您正在检查
x
Task的结果,该Task可以与t
相同,也可以是Task.Delay(TimeSpan.FromSeconds(3))
(前提是您await
它)。 You should be able to get the Result from t
instead: 您应该能够从
t
获得结果:
Task<string[]> t = new Task<string[]>(() => grabber.grab(link));
// ^^^^^^^^ also defining what the t.Result should contain
var x = await Task.WhenAny(t, Task.Delay(TimeSpan.FromSeconds(3)));
if (x == t){ // make sure that Task.WhenAny returned the t Task
{
string[] result = t.Result; // get t's Result, not x
foreach (string item in result)
{
list.Add(item);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.