42

I have an interface

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

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

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

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.

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?

EDIT

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.

Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
Anders Gustafsson
  • 15,837
  • 8
  • 56
  • 114
  • 2
    For this case, I believe `Task.FromResult(SynchronousBarCreator())` is better than `Task.Run(...)` as it doesn't actually schedule and run a task to get the result. – Steve Guidi Nov 05 '14 at 07:20
  • Is `SynchronousBarCreator` long running? Is it CPU bound, or does it spend its time waiting for something else? – CodesInChaos Nov 05 '14 at 09:51
  • In the case I am working on right now, it is *not* long running. But it might be in upcoming scenarios. – Anders Gustafsson Nov 05 '14 at 09:53
  • This is very confusing and it is not best practice. My suggestion are: **Create one Interface that has both Sync and ASync methods** a good example of this approach are **WebClient**, **EF**, etc,... or **Create 2 Interfaces** one for Sync and the other for Async methods. – Jaider Nov 02 '17 at 05:31

4 Answers4

28

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?

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Guillaume
  • 12,824
  • 3
  • 40
  • 48
  • Thanks for a very good summary, Guillaume. Is there even a context where you would recommend my first implementation (`async Task CreateBarAsync() { return SynchronousBarCreator(); }`) or is that approach *never* advisable? – Anders Gustafsson Nov 05 '14 at 08:37
  • 13
    You shouldn't use [*async over sync*](http://blogs.msdn.com/b/pfxteam/archive/2012/03/24/10287244.aspx) – Yuval Itzchakov Nov 05 '14 at 08:42
  • 2
    `Task.Run` is almost always over used. There really is only one reason ever to use it and that is when you have computation on a GUI application. But its very rare to have computationally (relative to the CPU) intensive tasks . – Aron Nov 05 '14 at 16:03
  • 2
    @Aron I agree that it is often over used. It can also be usefull when you have to call a third party library that is synchronous even if it does time consuming tasks that could have been done asynchronously. – Guillaume Nov 05 '14 at 16:19
24

Try this:

class Foo2 : IFoo
{
    public Task<Bar> CreateBarAsync()
    {
        return Task.FromResult<Bar>(SynchronousBarCreator());
    }
}

Task.FromResult creates an already completed task of the specified type using the supplied value.

NeddySpaghetti
  • 13,187
  • 5
  • 32
  • 61
  • 1
    Many thanks, Ned, this seems like a very good solution for my scenario. I was also looking for a solution that would work on .NET 4 with *async* extensions, and fortunately it turns out that the `TaskEx` class also contains a`FromResult` method. So all in all, I am very satisfied with this solution. – Anders Gustafsson Nov 05 '14 at 07:41
  • 3
    Glad I was able to help – NeddySpaghetti Nov 05 '14 at 07:42
  • Hey people, why do you rate this answer? This method runs synchronously but has Async suffix and returns Task, this is not correct at all. You should return Task.Run(SynchronousBarCreator) to make it asynchronous. – Alexander Danilov Jan 09 '16 at 11:48
  • +1 To support this answer, I checked what Microsoft does for its `MemoryStream.ReadAsync`, and that's exactly what they use. See https://referencesource.microsoft.com/#mscorlib/system/io/memorystream.cs,397 ; Could be useful to add it to your already good answer :) – yair Jun 10 '18 at 07:35
8

If you're using .NET 4.0, you can use TaskCompletionSource<T>:

Task<Bar> CreateBarAsync()
{
    var tcs = new TaskCompletionSource<Bar>();
    tcs.SetResult(SynchronousBarCreator());
    return tcs.Task
}

Ultimately, if theres nothing asynchronous about your method you should consider exposing a synchronous endpoint (CreateBar) which creates a new Bar. That way there's no surprises and no need to wrap with a redundant Task.

Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
  • Many thanks, Yuval, also good to know. This would even work without the *async* extensions from *Microsoft Async* NuGet package, right? I do however include the *async* extensions in my .NET 4 project, and then the `TaskEx.FromResult` method seems to be the more attractive approach. – Anders Gustafsson Nov 05 '14 at 07:53
  • 3
    Yes, you can use it without any extension. `Task.FromResult` and `TaskEx.FromResult` are simply a wrapper around a `TaskCompletionSource` – Yuval Itzchakov Nov 05 '14 at 08:01
  • There is a case for chaining a `Task.Run` (or awaiting a Task.Delay, or using ContinueWith), if this method will be async in the future - not performing it asynchronously now might create unpredictable execution order or cause race conditions. – Benjamin Gruenbaum Nov 05 '14 at 12:35
6

To complement others answers, there's one more option, which I believe works with .NET 4.0 too:

class Foo2 : IFoo
{
    public Task<Bar> CreateBarAsync()
    {
        var task = new Task<Bar>(() => SynchronousBarCreator());
        task.RunSynchronously();
        return task;
    }
}

Note task.RunSynchronously(). It might be the slowest option, compared to Task<>.FromResult and TaskCompletionSource<>.SetResult, but there's a subtle yet important difference: the error propagation behavior.

The above approach will mimic the behavior of the async method, where exception is never thrown on the same stack frame (unwinding it out), but rather is stored dormant inside the Task object. The caller actually has to observe it via await task or task.Result, at which point it will be re-thrown.

This is not the case with Task<>.FromResult and TaskCompletionSource<>.SetResult, where any exception thrown by SynchronousBarCreator will be propagated directly to the caller, unwinding the call stack.

I have a bit more detailed explanation of this here:

Any difference between "await Task.Run(); return;" and "return Task.Run()"?

On a side note, I suggest to add a provision for cancellation when designing interfaces (even if cancellation is not currently used/implemented):

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

class Foo2 : IFoo
{
    public Task<Bar> CreateBarAsync(CancellationToken token)
    {
        var task = new Task<Bar>(() => SynchronousBarCreator(), token);
        task.RunSynchronously();
        return task;
    }
}
Community
  • 1
  • 1
noseratio
  • 59,932
  • 34
  • 208
  • 486