[英]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
什么作用?
await
keywordawait
关键字await
s that are present in the body of the method.await
进行转换。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.编译器警告确实试图澄清这一点 - 没有任何
await
的async
方法只是简单的没有意义。 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.