【问题标题】:C# Async recursive function not working properly? [duplicate]C#异步递归函数不能正常工作? [复制]
【发布时间】:2020-12-15 10:10:16
【问题描述】:

这是我的代码:

  class Program
{
    static void Main(string[] args)
    {
        update();
    }

    static async void update()
    {
        await Task.Delay(100);
        Console.WriteLine("X");
        update();
    }
}

控制台从不输出任何文本,我也不知道为什么。我做错了什么?

【问题讨论】:

标签: c# asynchronous recursion


【解决方案1】:

您的Main 方法不是async,因此它不会等待您的update 方法。此外,您的update 方法应该返回一个Task,以便您的Main 方法可以等待它。

static async Task Main(string[] args)
{
    await update();
}

static async Task update()
{
    await Task.Delay(100);
    Console.WriteLine("X");
    await update();
}

【讨论】:

  • @Selvin 谢谢我会更新答案
猜你喜欢
  • 2018-06-17
  • 2022-12-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-08
  • 2021-09-30
相关资源
最近更新 更多