c# - C#异步递归函数不能正常工作?
问题描述
这是我的代码:
class Program
{
static void Main(string[] args)
{
update();
}
static async void update()
{
await Task.Delay(100);
Console.WriteLine("X");
update();
}
}
控制台从不输出任何文本,我也不知道为什么。我究竟做错了什么?
解决方案
你的Main
方法不是async
,所以它不会等待你的update
方法。此外,您的update
方法应该返回 aTask
以便您的Main
方法可以等待它。
static async Task Main(string[] args)
{
await update();
}
static async Task update()
{
await Task.Delay(100);
Console.WriteLine("X");
await update();
}
推荐阅读
- python - 使用 mysql 时规范 url 的问题
- google-smart-home - 谷歌智能家居通过用户调用“同步”意图
- python - pyodbc vs ADO 通过 COM 用于 Python 脚本
- android - 如何在启动画面中显示动画 GIF?
- google-cloud-platform - Google Dataprep 与消息代理的集成
- symfony - Websocket 在订阅时断开连接
- variables - RobotFramework - 在不同的环境中运行测试
- javascript - 运行 webpack 时有效正则表达式中的意外令牌
- string - POWERSHELL :拆分字符串,如 awk
- algorithm - 查找无向图上的所有路径