0

Would like to confirm something related to he ConfigureAwait. As far a i read by default Configureawait is set to true. However it can be set to false. There is something people saying that when it's true (default) then all code after await will be run within context, and if is set to false it will not. So according to the example if i set it to false then DoIndependentWork would fail to write to a control unless i do Dispatcher as it will not run on context means on UI thread in this case, and if true it will. Is it right thinking? So generally saying when false is set all after await will not be running on main thread (not within context) so for example access to the controls will be not possible (unless dispatcher of course). Is it right? I used to use think of it like if it's WIndows forms, then context is UI thread, so simply saying if i set false means it will fail to access controls, if true it will work. Is it all rght thinking? And what about console application/windows service, what is the context for that and does it really matter what i set either true or false?

 Async Function AccessTheWebAsync1() As Task(Of Integer)
        Dim getStringTask As Task(Of String) = New HttpClient().GetStringAsync("http://msdn.microsoft.com")

        Dim urlContents As String = Await getStringTask.ConfigureAwait(false)

        DoIndependentWork()

        Return urlContents.Length
    End Function

    Sub DoIndependentWork()

        If ResultsTextBox.Dispatcher.CheckAccess() Then
            Write()
        Else
            ResultsTextBox.Dispatcher.Invoke(New Action(AddressOf Write))
        End If

        'ResultsTextBox.Text &= "Working . . . . . . ." & vbCrLf
    End Sub
2
  • why -1 ? Is it somehing wrong with my question? Commented May 21, 2017 at 8:15
  • Not sure about the -1 it's not me and I can't see why to be honest. However it's worth noting you've a lot of questions which have answers and have yet to be marked as accepted. Please take the tour to see how the site works. You also gain 2 rep for each accepted answer. You can then look at upvoting which is a way of saying thanks. Commented May 22, 2017 at 12:45

1 Answer 1

2

I recommend you read my async intro post. It answers your questions.

There is something people saying that when it's true (default) then all code after await will be run within context, and if is set to false it will not.

Almost true. To quote my intro:

Await is like a unary operator: it takes a single argument, an awaitable... Await examines that awaitable to see if it has already completed; if the awaitable has already completed, then the method just continues running (synchronously, just like a regular method).

If await sees that the awaitable has not completed, then it acts asynchronously... If you’re awaiting a built-in awaitable (such as a task), then the remainder of the async method will execute on a “context” that was captured before the await returned.

So, the context is only captured if:

  1. You await a Task/Task<T>.
  2. That task is not already completed.

If both of these are true, then the context is captured (by default).

if it's WIndows forms, then context is UI thread, so simply saying if i set false means it will fail to access controls, if true it will work.

That "context" is SynchronizationContext.Current, unless it is null, in which case it is TaskScheduler.Current.

This generally means either a UI context, ASP.NET request context, or the thread pool context. To quote my intro:

What exactly is that “context”? Simple answer: If you’re on a UI thread, then it’s a UI context. If you’re responding to an ASP.NET request, then it’s an ASP.NET request context. Otherwise, it’s usually a thread pool context.

And what about console application/windows service, what is the context for that and does it really matter what i set either true or false?

Since those environments do not provide a custom SynchronizationContext nor a custom TaskScheduler, they use the thread pool context. So it doesn't matter whether you use ConfigureAwait(false) or not.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks i read your blog and think what i wrote here is true :) Just to keep in mind when setting false for Win forms/wpf/asp.net all after that await set cnfigure await to false within async method will be run no on context (UI thread) so means for example control cannot be accessed (unless i make dispatcher). If that win service/console doesn;t matter what is set. I think my understanding is correct know.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.