13

Given the following extension to prevent Tasks from blocking the UI Thread ( probably not the exact correct terminology but whatever ) :

    public static ConfiguredTaskAwaitable DontBlock( this Task T ) {
        return T.ConfigureAwait( false );
    }

    public static ConfiguredTaskAwaitable<T> DontBlock<T>( this Task<T> T2 ) {
        return T2.ConfigureAwait( false );
    }

In some cases ( such as if I need to call an awaitable within an object constructor, or if I need to call .Wait( ) from a WinForms Program.Main( ) method), I need to do the following :

public class Foo{
    public Foo( ){
        //I know full well that the result from DontBlock does not have a 'wait' method, so of course this will fail miserably.
        AwaitableBar.DontBlock( ).Wait( );
    }
}

How can I 'await' it outside an async function/method if I can't call .Wait( ) on it? I see that it has a .GetAwaiter( ) method which returns a ConfiguredTaskAwaiter object, but I have no idea what to do with it, and Google has failed me again... Am I supposed to call GetResult( ) on the returned object right away? Will doing that wait for everything to finish, or just skip right over it, or explode?

5
  • if you wait for something outside an async function it will block the UI no matter how the task was configured.
    – Gusman
    Feb 7, 2016 at 0:00
  • 1
    The key methods in await are IsCompleted, GetResult() if that is true, and OnCompleted(Action) if that is false. However, doing this is much harder than just using await. If you absolutely need to Wait(), then... well, frankly you shouldn't be using an async-style method in the first place, but you could create any blocking primitive and use OnCompleted to release it when the task completes. Feb 7, 2016 at 0:16
  • @MarcGravell In most cases I don't need to call .Wait( ); in most cases I do use the full await/async all the way to the bottom (and thanks to one of your posts on another question which I read which mentioned the ConcigureAwaitable, that is now a staple in my design philosophy); however, in some cases (READ : WinForms Program.Main( ), Object Constructors), I do find it necessary to use an awaitable outside of an asynchronous context...
    – Will
    Feb 7, 2016 at 0:18
  • 3
    @Will frankly, I would suggest refactoring so that you don't need to do it in the constructor, etc Feb 7, 2016 at 0:36
  • 1
    This is totally off topic but I advise against using this whitespace style. it deviates from the community standard for no discernible reason. Conformance is the most important goal for code style.
    – usr
    Feb 7, 2016 at 0:40

2 Answers 2

11

I agree with the @usr that returning a ConfiguredTaskAwaitable is not ideal. Generally, your asynchronous methods should return tasks. This is a matter of separation of concerns.

Once you have a ConfiguredTaskAwaitable, though, simply call awaitableObj.GetAwaiter().GetResult() (or the appropriate substitution). GetResult() will block.

3
  • 1
    Regardless of how hair brained and inane my question is, this is the answer for which I was looking; Thank you.
    – Will
    Feb 7, 2016 at 2:00
  • I am working with a WPF + ASP.NET web api application and since project is too big to change in a one go, I am working with async along with ConfigureAwait(false) to wait on it so that rest of call hierarchy is unchanged. But when I call GetResult on ConfiguredTaskAwaitable debugger just disappears and nothing is returned to client application. Any help? Aug 19, 2018 at 18:45
  • @AshutoshSingh Sounds like you are blocking. Are you sure the task completes? GetResult is a blocking call that waits for completion of the task. Oct 31, 2018 at 19:26
11

Probably, you should not return a ConfiguredTaskAwaitable from anything. This is a helper type that is only needed in the await t.ConfigureAwait( false) pattern. await t.DontBlock() is also fine but don't pass this type around. I see no purpose in doing so.

You seem to believe that ConfigureAwait(false) unblocks the UI thread. Not so. Waiting always blocks a thread. Blocking means that this thread cannot continue execution. Wait causes that condition if the underlying task is not completed.

So probably, what you wanted to accomplish doing that is not accomplished. Use await on the UI thread. Use Wait() on non-UI-threads if you want to block.

AwaitableBar.DontBlock( ).Wait() tell me you have some wrong belief. Here, DontBlock does not unblock anything. It does nothing even if this code worked.

If it was that easy to unblock a thread, why would we need await at all?!

1
  • I found this question looking to do exactly what Will is talking about, and DontBlock is probably a misnomer and the more accurate statement would be DontDeadlock. If on the UI thread you synchronously Wait() on an async method that does not use ConfigureAwait(false), then you will end up deadlocking the UI thread. Sometimes you end up needing to synchronously wait like in constructors or Dispose methods. Mar 16, 2022 at 19:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.