C# 异步延时执行

https://blog.csdn.net/xiawu1990/article/details/78350253?utm_source=blogxgwz7

var t = Task.Run(async delegate
{
    await Task.Delay(5000);
    Console.WriteLine("5秒后会执行此输出语句");
    return 42;
});

Console.WriteLine("此输出语句立马执行");
t.Wait();
Console.WriteLine("Task t Status: {0}, Result: {1}", t.Status, t.Result);
Console.ReadLine();

慎用thread.sleep(),会线程阻塞,等待期间什么也干不了,相当于程序死了。

猜你喜欢

转载自www.cnblogs.com/wsq-blog/p/10661433.html