简体   繁体   English

此异步方法缺少“等待”运算符,将同步运行

[英]This async method lacks 'await' operators and will run synchronously

my program has 3 warnings of the following statement:我的程序有以下语句的 3 个警告:

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.考虑使用 'await' 运算符来等待非阻塞 API 调用,或使用 'await Task.Run(...)' 在后台线程上执行 CPU 密集型工作。

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");
}

The async keyword, by itself, doesn't really do much. async关键字本身并没有多大作用。 Remove it from your code and your code will act exactly the same.从您的代码中删除它,您的代码将完全相同。

What does async do? async什么作用?

  • It changes what's valid inside of the method, specifically it allows you to use the await keyword它改变了方法内部的有效内容,特别是它允许您使用await关键字
  • In turn, it means that the body of the method will be transformed, based on the await s that are present in the body of the method.反过来,这意味着方法的主体将根据方法主体中存在的await进行转换。
  • And if the method returns a value, the method is also transformed to wrap the return value in a Task .如果该方法返回一个值,该方法也会被转换为将返回值包装在一个Task

However, if you a) Don't have any await s in your method body and b) are void returning, then nothing special will be achieved.但是,如果你 a) 在你的方法体中没有任何await并且 b) 是void返回,那么不会有什么特别的东西。 The compiler warning does try to be clear about this - an async method without any await s just plain doesn't make sense.编译器警告确实试图澄清这一点 - 没有任何awaitasync方法只是简单的没有意义。 await s are the more important part of this feature. await是此功能中更重要的部分。

if you are overriding and async method with a sync method you can :如果您使用同步方法覆盖和异步方法,您可以:

await Task.Run(() => [YOUR SYNC METHOD]); await Task.Run(() => [你的同步方法]);

Yes, your code will probably use multi-threading.是的,您的代码可能会使用多线程。 However, it would still do so if you just removed the async keyword.但是,如果您只是删除async关键字,它仍然会这样做。 As you did not explain why it's there, I suggest removing it.由于您没有解释为什么它在那里,我建议将其删除。

If you want an async/await pattern, you can use Task.Delay() but I would suggest you read more on async/await before using it:如果你想要一个 async/await 模式,你可以使用Task.Delay()但我建议你在使用它之前阅读更多关于 async/await 的内容:

static async void Work3()
{
    Console.WriteLine("5 started");
    await Task.Delay(5000);
    Console.WriteLine("5 completed");
}

You have used ' async ' keyword with method which indicates that Work1(),Work2() and Work3() methods are executed asynchronously,but you have not used 'await' keyword.So it executed as synchronously.Use ' await ' keyword if you want to execute it asynchronously.您使用了“ async ”关键字和方法,表明 Work1()、Work2() 和 Work3() 方法是异步执行的,但您没有使用“ await ”关键字。所以它是同步执行的。使用“ await ”关键字如果你想异步执行它。

 static async void Work1()
 {
     Console.WriteLine("10 started");
     await Task.Delay(10000);
     Console.WriteLine("10 completed");
 }

 static async void Work2()
 {
     Console.WriteLine("3 started");
     await Task.Delay(3000);
     Console.WriteLine("3 completed");
 }

 static async void Work3()
 {
     Console.WriteLine("5 started");
     await Task.Delay(5000);
     Console.WriteLine("5 completed");
 }

您使用关键字async指定了方法(Work1、Work2、Work3),但这些方法中的任何代码都没有使用await运算符来调用异步调用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何删除StyleCop警告“此异步方法缺少'await'运算符,将同步运行”,而不会从签名中删除异步 - How to remove StyleCop warning “This async method lacks 'await' operators and will run synchronously” without removing async from signature 我是否应该担心“此异步方法缺少 'await' 运算符并将同步运行”警告 - Should I worry about "This async method lacks 'await' operators and will run synchronously" warning Task.WaitAll 的使用没有任何“等待”运算符导致警告 CS1998 此异步方法缺少“等待”运算符,将同步运行 - Usage of Task.WaitAll without any 'await' operators causing warning CS1998 This async method lacks 'await' operators and will run synchronously ASP.NET 内核 Web API - 如何解决警告 CS1998 此异步方法缺少“等待”运算符,将同步运行 - ASP.NET Core Web API -How to resolve Warning CS1998 This async method lacks 'await' operators and will run synchronously OWIN 身份验证:异步方法缺少“等待”运算符 - OWIN Authentication:async method lacks 'await' operators 异步方法缺少等待 - The async method lacks await 异步方法中的警告消息说它缺少等待运算符 - Warning message in async method saying that it lacks await operators 当等待在传递给异步lambda表达的函数中时,异步方法缺少等待操作员警告 - Async method lacks await operators warning when the await is in a function passed to an async lambda expresion c# 异步等待奇怪的警告 CS1998:此异步方法缺少“等待”运算符 - c# async await strange warning CS1998: This async method lacks 'await' operators 异步无效方法缺少等待 - async void method lacks await
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM