Threading.Tasks.Task'不包含'Result'的定义 [英] Threading.Tasks.Task' does not contain a definition for 'Result'

查看:63
本文介绍了Threading.Tasks.Task'不包含'Result'的定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在尝试学习如何使用Task's进行编程,并且正在做练习:

So i'm trying to learn how to program with Task's and i'm doing an exercise:

public static int ReturnFirstResult(Func<int>[] funcs)
{
        Task[] tasks = new Task[funcs.Length];
        for (int i = 0; i < funcs.Length; i++)
        {
            tasks[i] = CreatingTask(funcs[i]);
        }
        return Task<int>.Factory.ContinueWhenAny(tasks, (firstTask) =>
                                                            {
                                                                Console.WriteLine(firstTask.Result);
                                                                return ***????***;
                                                            }).***Result***;
}
private static Task CreatingTask(Func<int> func)
{
        return Task<int>.Factory.StartNew(() => { return func.Invoke(); });
}

我要提供一系列Func来运行,其思想是返回已完成的第一个func的结果.问题在于结果"字段不可用...

I'm giving a array of Funcs to run, the ideia is to returns the result of the first that func that was done. The problem is that the field Result is not available...

我在这里想念什么?

推荐答案

您要从 CreatingTask 方法返回 Task -您需要返回 Task<int> ,然后将 tasks 更改为 Task< int> [] ,而不是 Task [] .

You're returning Task from the CreatingTask method - you need to return Task<int>, and then change tasks to be Task<int>[] instead of Task[].

基本上, Task 表示不产生结果的任务-而 Task< T> 表示产生 T类型的结果的任务代码>.对于您而言,整个代码中的所有内容都返回 int ,因此您到处都需要 Task< int> .

Basically, Task represents a task which doesn't produce a result - whereas Task<T> represents a task producing a result of type T. In your case, everything throughout your code returns int, so you need Task<int> everywhere.

这篇关于Threading.Tasks.Task'不包含'Result'的定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆