这种异步方法缺少'await'运算符并将同步运行(This async method lacks 'await' operators and will run

编程入门 行业动态 更新时间:2024-10-25 16:30:39
这种异步方法缺少'await'运算符并将同步运行(This async method lacks 'await' operators and will run synchronously)

我的程序有以下声明的3个警告:

这种异步方法缺少'await'运算符并将同步运行。 考虑使用'await'运算符等待非阻塞API调用,或'await Task.Run(...)'在后台线程上执行CPU绑定工作。

试着告诉我什么警告? 我该怎么办?

这是我的代码:它是否使用多线程运行?

static void Main(string[] args) { Task task1 = new Task(Work1); Task task2 = new Task(Work2); Task task3 = new Task(Work3); task1.Start(); task2.Start(); task3.Start(); Console.ReadKey(); } static async void Work1() { Console.WriteLine("10 started"); Thread.Sleep(10000); Console.WriteLine("10 completed"); } static async void Work2() { Console.WriteLine("3 started"); Thread.Sleep(3000); Console.WriteLine("3 completed"); } static async void Work3() { Console.WriteLine("5 started"); Thread.Sleep(5000); Console.WriteLine("5 completed"); }

my program has 3 warnings of the following statement:

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

What is the warning try to tell me? What should I do?

This is my code: Is it running using multi-threading?

static void Main(string[] args) { Task task1 = new Task(Work1); Task task2 = new Task(Work2); Task task3 = new Task(Work3); task1.Start(); task2.Start(); task3.Start(); Console.ReadKey(); } static async void Work1() { Console.WriteLine("10 started"); Thread.Sleep(10000); Console.WriteLine("10 completed"); } static async void Work2() { Console.WriteLine("3 started"); Thread.Sleep(3000); Console.WriteLine("3 completed"); } static async void Work3() { Console.WriteLine("5 started"); Thread.Sleep(5000); Console.WriteLine("5 completed"); }

最满意答案

async关键字本身并没有做太多。 从代码中删除它,您的代码将完全相同。

async做什么?

它改变了方法内部的有效内容,特别是它允许您使用await关键字 反过来,这意味着该方法的主体将根据方法体中存在的await s进行转换。 如果该方法返回一个值,则该方法也会转换为将返回值包装在Task 。

但是,如果你a)你的方法体中没有任何await ,并且b)返回void ,那么就没有什么特别的了。 编译器警告确实试图清楚这一点 - 没有任何await的async方法只是简单没有意义。 await s是此功能中更重要的部分。

The async keyword, by itself, doesn't really do much. Remove it from your code and your code will act exactly the same.

What does async do?

It changes what's valid inside of the method, specifically it allows you to use the await keyword In turn, it means that the body of the method will be transformed, based on the awaits that are present in the body of the method. And if the method returns a value, the method is also transformed to wrap the return value in a Task.

However, if you a) Don't have any awaits in your method body and b) are void returning, then nothing special will be achieved. The compiler warning does try to be clear about this - an async method without any awaits just plain doesn't make sense. awaits are the more important part of this feature.

更多推荐

本文发布于:2023-07-16 09:57:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1126826.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:并将   运算符   方法   await   async

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!