我是否应该担心“此异步方法缺少'await'运算符,并且将同步运行”警告 [英] Should I worry about "This async method lacks 'await' operators and will run synchronously" warning

查看:1713
本文介绍了我是否应该担心“此异步方法缺少'await'运算符,并且将同步运行”警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个暴露一些异步方法的接口。更具体地说,它具有定义的返回Task或Task T的方法。我正在使用async / await关键字。

I have a interface which exposes some async methods. More specifically it has methods defined which return either Task or Task<T>. I am using the async/await keywords.

我正在实现此接口。但是,在其中某些方法中,此实现没有任何等待。因此,我收到编译器警告此异步方法缺少'await'运算符,将同步运行...

I am in the process of implementing this interface. However, in some of these methods this implementation doesn't have anything to await. For that reason I am getting the compiler warning "This async method lacks 'await' operators and will run synchronously..."

我知道为什么会收到错误,但是我想知道在这种情况下我是否应该对它们做任何事情。

I understand why I am getting the error but am wondering whether I should do anything about them in this context. It feels wrong to ignore compiler warnings.

我知道我可以通过等待Task.Run来解决它,但这对于只做一些便宜的方法来说是错误的操作。听起来还好像会给执行增加不必要的开销,但是我也不知道是否已经存在,因为存在async关键字。

I know I can fix it by awaiting on a Task.Run but that feels wrong for a method that is only doing a few inexpensive operations. It also sounds like it will add unneeded overhead to the execution but then I am also not sure if that is already there because the async keyword is present.

我应该忽略吗

推荐答案

异步 >关键字仅仅是方法的实现细节;它不是方法签名的一部分。如果一个特定的方法实现或替代方法没有什么可等待的,则只需省略 async 关键字,然后使用 Task.FromResult< TResult>

The async keyword is merely an implementation detail of a method; it isn't part of the method signature. If one particular method implementation or override has nothing to await, then just omit the async keyword and return a completed task using Task.FromResult<TResult>:

public Task<string> Foo()               //    public async Task<string> Foo()
{                                       //    {
    Baz();                              //        Baz();
    return Task.FromResult("Hello");    //        return "Hello";
}                                       //    }

如果您的方法返回任务而不是任务< TResult> ,则您可以返回任何类型和值的已完成任务。 Task.FromResult(0)似乎是一个受欢迎的选择:

If your method returns Task instead of Task<TResult>, then you can return a completed task of any type and value. Task.FromResult(0) seems to be a popular choice:

public Task Bar()                       //    public async Task Bar()
{                                       //    {
    Baz();                              //        Baz();
    return Task.FromResult(0);          //
}                                       //    }

或者,从.NET Framework 4.6开始,您可以返回 Task.CompletedTask

Or, as of .NET Framework 4.6, you can return Task.CompletedTask:

public Task Bar()                       //    public async Task Bar()
{                                       //    {
    Baz();                              //        Baz();
    return Task.CompletedTask;          //
}                                       //    }

这篇关于我是否应该担心“此异步方法缺少'await'运算符,并且将同步运行”警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆