小编Pat*_*ick的帖子

如何获取异步方法的返回值?

您好,我正在尝试了解任务和异步方法的概念。我一直在玩这个代码一段时间无济于事。有人能告诉我如何从 test() 方法中获取返回值并将该值分配给变量吗?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication2
{
   class Program
   {

    static  void Main(string[] args)
    {

        Task test1 = Task.Factory.StartNew(() => test());
        System.Console.WriteLine(test1);
        Console.ReadLine();
    }

    public static async Task<int> test()
    {
        Task t = Task.Factory.StartNew(() => {   Console.WriteLine("do stuff"); });
        await t;
        return 10;
    }
}
}
Run Code Online (Sandbox Code Playgroud)

.net async-await

5
推荐指数
1
解决办法
3011
查看次数

标签 统计

.net ×1

async-await ×1