简体   繁体   English

有Task的麻烦获得Task结果(无法将System.Threading.Task.Task隐式转换为string [])

[英]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);
    }
}

Created a small reproducable example here 在此处创建了一个可复制的小示例

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将 Microsoft.Azure.Cosmos.Table.DynamicTableEntity 转换为 System.Threading.Task.Task <microsoft.azure.cosmos.table.dynamictableentity></microsoft.azure.cosmos.table.dynamictableentity> - Cannot convert Microsoft.Azure.Cosmos.Table.DynamicTableEntity to System.Threading.Task.Task<Microsoft.Azure.Cosmos.Table.DynamicTableEntity> 无法将类型&#39;void&#39;隐式转换为&#39;System.Threading.Tasks.Task&#39; - Cannot implicitly convert type 'void' to 'System.Threading.Tasks.Task' 无法隐式转换类型&#39;System.Threading.Tasks.Task - Cannot implicitly convert type 'System.Threading.Tasks.Task 无法将类型&#39;bool&#39;隐式转换为&#39;System.Threading.Tasks.Task&#39; - Cannot implicitly convert type 'bool' to 'System.Threading.Tasks.Task' 无法隐式转换类型&#39;System.Threading.Tasks.Task <String> “串” - Cannot implicitly convert type 'System.Threading.Tasks.Task<String> to 'string' 错误 CS0029:无法将类型“字符串”隐式转换为“System.Threading.Tasks.Task”<string> '</string> - Error CS0029: Cannot implicitly convert type 'string' to 'System.Threading.Tasks.Task<string>' 无法将类型“字符串”隐式转换为“System.Threading.Tasks.Task”<string> &#39; - Cannot implicitly convert type 'string' to 'System.Threading.Tasks.Task<string>' 无法将类型“字符串”隐式转换为“System.Threading.Tasks.Task”<string> ' 在 c#</string> - Cannot implicitly convert type 'string' to 'System.Threading.Tasks.Task<string>' in c# 如何从异步 function C# 返回字符串而不是 SYSTEM.THREADING.TASK.TASK`1[SYSTEM.STRING] - How can I return a string rather than SYSTEM.THREADING.TASK.TASK`1[SYSTEM.STRING] from an async function C# 无法隐式转换类型&#39;System.Threading.Tasks.Task <object> 在SignalR中从&#39;到&#39;string&#39; - Cannot implicitly convert type 'System.Threading.Tasks.Task<object>' to 'string' in SignalR
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM