如何实现返回Task<T>的接口方法? [英] How to implement interface method that returns Task&lt;T&gt;?

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

问题描述

我有一个界面

interface IFoo
{
  Task<Bar> CreateBarAsync();
}

创建Bar有两种方法,一种是异步的,一种是同步的.我想为这两种方法中的每一种提供一个接口实现.

There are two methods to create Bar, one asynchronous and one synchronous. I want to provide an interface implementation for each of these two methods.

对于异步方法,实现可能如下所示:

For the asynchronous method, the implementation could look like this:

class Foo1 : IFoo
{
  async Task<Bar> CreateBarAsync()
  {
    return await AsynchronousBarCreatorAsync();
  }
}

但是我应该如何实现使用同步方法创建BarFoo2类?

But HOW should I implement the class Foo2 that uses the synchronous method to create Bar?

可以实现同步运行的方法:

I could implement the method to run synchronously:

  async Task<Bar> CreateBarAsync()
  {
    return SynchronousBarCreator();
  }

然后编译器会警告不要在方法签名中使用async:

The compiler will then warn against using async in the method signature:

这种异步方法缺少await"操作符,将同步运行.考虑使用await"运算符来等待非阻塞 API 调用,或使用await Task.Run(...)"在后台线程上执行 CPU 密集型工作.

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.

或者,我可以实现显式返回Task的方法.在我看来,代码的可读性会降低:

Or, I could implement the method to explicitly return Task<Bar>. In my opinion the code will then look less readable:

  Task<Bar> CreateBarAsync()
  {
    return Task.Run(() => SynchronousBarCreator());
  }

从性能的角度来看,我认为两种方法的开销大致相同,或者?

From a performance point of view, I suppose both approaches have about the same overhead, or?

我应该选择哪种方法;同步实现 async 方法还是将同步方法调用显式包装在 Task 中?

Which approach should I choose; implement the async method synchronously or explicitly wrap the synchronous method call in a Task?

编辑

我正在处理的项目实际上是一个 .NET 4 项目,带有来自 Microsoft Async NuGet 包.在 .NET 4 上,Task.Run 可以替换为 TaskEx.Run.上面例子中我有意识地使用了.NET 4.5的方法,希望能把主要问题讲得更清楚.

The project I am working on is really a .NET 4 project with async / await extensions from the Microsoft Async NuGet package. On .NET 4, Task.Run can then be replaced with TaskEx.Run. I consciously used the .NET 4.5 method in the example above in the hope of making the primary question more clear.

推荐答案

当您必须从接口实现异步方法并且您的实现是同步的时,您可以使用 Ned 的解决方案:

When you have to implement an async method from an interface and your implementation is synchronous, you can either use Ned's solution:

public Task<Bar> CreateBarAsync()
{
    return Task.FromResult<Bar>(SynchronousBarCreator());
}

使用此解决方案,该方法看起来是异步的,但实际上是同步的.

With this solution, the method looks async but is synchronous.

或者你提出的解决方案:

Or the solution you proposed:

  Task<Bar> CreateBarAsync()
  {
    return Task.Run(() => SynchronousBarCreator());
  }

这种方法是真正异步的.

This way the method is truly async.

您没有一个通用的解决方案来匹配如何实现返回任务的接口方法"的所有情况.这取决于上下文:您的实现是否足够快,因此在另一个线程上调用它是无用的?何时调用此方法(它会冻结应用程序)如何使用此接口?甚至可以在另一个线程中调用您的实现吗?

You don't have a generic solution that will match all cases of "How to implement interface method that returns Task". It depends on the context: is your implementation fast enough so invoking it on another thread is useless? How is this interface used a when is this method invoked (will it freeze the app)? Is it even possible to invoke your implementation in another thread?

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

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