返回Task的接口的同步实现 [英] Synchronous implementation of interface that returns Task

查看:29
本文介绍了返回Task的接口的同步实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

类似于实现一个接口这需要同步代码中的 Task 返回类型,尽管我很好奇是否应该忽略我的情况生成的编译器错误.

Similar to Implementing an interface that requires a Task return type in synchronous code although I'm curious if I should just ignore the compiler error my situation generates instead.

假设我有一个这样的界面:

Let's say I have an interface like this:

public interface IAmAwesome {
    Task MakeAwesomeAsync();
}

在某些实现中,使用 asyncawait 异步完成会带来极大的好处.这确实是界面试图允许的.

In some implementations making awesome benefits from being done asynchronously using async and await. This is really what the interface is attempting to allow for.

在其他情况下,也许很少见,只需要一个简单的同步方法就可以使很棒.所以让我们假设实现看起来像这样:

In other cases, perhaps rare, only a simple synchronous method is needed to make awesome. So let's suppose that implementation looks like this:

public class SimplyAwesome : IAmAwesome {
    public async Task MakeAwesomeAsync() {
        // Some really awesome stuff here...
    }
}

这可行,但编译器发出警告:

This works, but the compiler is warning:

此方法缺少await"运算符,将同步运行.考虑使用 await 运算符来等待非阻塞 API 调用,或 'await TaskEx.Run(...)' 在后台执行受 CPU 限制的工作线程.

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

编译器实际上是在建议这个解决方案:

The compiler is actually suggesting this solution:

public class SimplyAwesome : IAmAwesome {
    public async Task MakeAwesomeAsync() {
        await Task.Run(() => {
            // Some really awesome stuff here, but on a BACKGROUND THREAD! WOW!
        });
    }
}

我的问题是 - 什么时候我选择忽略这个编译器警告?在某些情况下,这项工作非常简单,以至于为其创建线程无疑会适得其反.

My question is - what should determine when I choose to ignore this compiler warning? In some cases the work is so simple that spawning a thread for it is undeniably counter-productive.

推荐答案

如果你真的想同步完成工作,你知道你的 async 方法将始终同步运行,这在这种情况,那么无论如何,忽略警告.如果您理解警告告诉您的内容并认为它所描述的操作是正确的,那么这不是问题.这是一个警告而不是一个错误是有原因的.

If you really do want to do the work synchronously, you know that your async method will always run synchronously, and that's desirable in the situation, then by all means, ignore the warning. If you understand what the warning is telling you and feel that the action it is describing is correct, then it's not a problem. There's a reason it's a warning and not an error after all.

当然,另一种选择是不使方法 async 并简单地使用 Task.FromResult 返回已完成的任务.它会改变错误处理语义(除非您还捕获所有异常并将它们包装到您返回的任务中),因此至少要注意这一点.如果您确实希望通过生成的 Task 传播异常,则可能值得离开方法 async 并仅抑制警告.

Of course, the other option is to just not make the method async and to simply use Task.FromResult to return an already completed task instead. It would change the error handling semantics (unless you also catch all exceptions and wrap them into a task that you return) so at least be mindful of that. If you really want exceptions to be propagated through the resulting Task, it may be worth leaving the method async and just suppressing the warning.

这篇关于返回Task的接口的同步实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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