c# - C# 中的 Async 和 Await 以及问题

标签 c# .net async-await console-application

这个程序没有以正确的顺序打印输出。

public static void Main(string[] args)
    {
        new Program().Start();
    }

    public async void Start()
    {
        int num1 = await GetNumber();
        int num2 = await GetNumber();
        int num3 = await GetNumber();
        Console.WriteLine("Wait...");
        Console.ReadKey();
    }

    public static async Task<int> GetNumber()
    {
        System.Threading.Thread.Sleep(4000);
        Console.WriteLine("Hello");
        return 0;
    }

输出:

--------wait 4Seconds
--------print Hello
--------wait 4Seconds
--------print Hello
--------wait 4Seconds
--------print Hello
--------print wait....

它应该输出

--------print wait....
--------wait 4Seconds
--------print Hello
--------print Hello
--------print Hello

最佳答案

使用

Await Task.Delay(Timespan.FromMilliSeconds (4000))

代替 Thread.Sleep。

完整的示例。

using System;
using System.Threading.Tasks;

namespace Brad
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var task = new Program().Start();
            Console.WriteLine("Wait...");
            // You have to put a synchronous Wait() here because
            // Main cannot be declared as async
            task.Wait();
        }

        public async Task Start()
        {
            int num1 = await GetNumber();
            int num2 = await GetNumber();
            int num3 = await GetNumber();
            Console.WriteLine("Finished");
        }

        public static async Task<int> GetNumber()
        {
            await Task.Delay(TimeSpan.FromMilliseconds(400));
            Console.WriteLine("Hello");
            return 0;
        }
    }
}

你可以看到它在这里运行

https://dotnetfiddle.net/KHJaDZ

或者您可能希望任务并行运行,而不是一个接一个地运行。你可以试试

using System;
using System.Threading.Tasks;

namespace Brad
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var task = new Program().Start();
            Console.WriteLine("Wait...");
            // You have to put a synchronous Wait() here because
            // Main cannot be declared as async
            task.Wait();
        }

        public async Task Start()
        {
            var task1 = GetNumber();
            var task2 = GetNumber();
            var task3 = GetNumber();
            // This runs the tasks in parallel
            await Task.WhenAll(task1, task2, task3);
            Console.WriteLine("Finished");
        }

        public static async Task<int> GetNumber()
        {
            await Task.Delay(TimeSpan.FromMilliseconds(400));
            Console.WriteLine("Hello");
            return 0;
        }
    }
}

这是在这里运行的。

https://dotnetfiddle.net/kVk77Z

关于c# - C# 中的 Async 和 Await 以及问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43405260/

相关文章:

javascript - 调用异步静态函数时出现语法错误

c# - 使用 IP 地址打印到网络打印机

.net - 这个 hack 是 T4 的定义行为吗

.net - 如何获取 WinXP 版 FxCop?

c# - 如何在后台继续执行 Task、await 和 async

c# - 在 C# 中将 void 转换为异步返回

c# - 最小起订量如何正确模拟仅设置属性

c# - 不同泛型的数组

c# - 多态性运行时对象类型检查

javascript - 如何在 JavaScript 中使用带有 ILIST<models> 的 foreach 循环?