如何实现返回的任务和LT接口方法; T>? [英] How to implement interface method that returns Task<T>?

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

问题描述

我有一个接口

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

有两种方法来创建酒吧一异步和一个同步的。我想要提供一个接口实现为每个这两种方法的

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

不过,我应该如何实现类 foo2的,它使用的同步的方法来创建酒吧

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

,编译器会发出警告,然后对方法签名使用异步

这异步方法缺乏'等待'运营商,并会同步运行。请考虑使用'等待'操作,以等待非阻塞API调用,或等待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.

或者说,我的可以的实施方法明确地返回任务&LT;酒吧&GT; 。在我看来,code将如下的可读性:

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?

我应该选择哪一种方法;贯彻落实异步法同步或明确包裹同步方法调用在工作

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

修改

我工作的这个项目确实是一个.NET 4项目的异步 / 计谋的距离的微软异步的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?

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

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