ConfigureAwait(false) in F#

685 Views Asked by At

I am using a library which has been written in C# and uses the async/await pattern. In C# I can await something by calling it with ConfigureAwait(false) but when I use the library from F# I don't see a way of doing the same thing?

Currently I do SomeMethodAsync() |> Async.AwaitTask to convert it into an Async F# workflow, but how can I do the same as ConfigureAwait(false) from F# as well when calling SomeMethodAsync?


EDIT:

With some awesome help in the F# Slack channel and some additional googling I found a good explanation of how to switch context here: http://tomasp.net/blog/async-non-blocking-gui.aspx/

1

There are 1 best solutions below

0
On

It's as simple as calling ConfigureAwait before converting it to an F# Task.

SomeMethodAsync().ConfigureAwait(false) |>  Async.AwaitTask

However, if you are mostly dealing with C# async tasks. It's cleaner to us the TaskBuilder.fs computation expression. Available in nuget using dotnet add package TaskBuilder.fs.

You don't have to manually call ConfigureAwait(false) instead just

open FSharp.Control.Tasks.V2.ContextInsensitive

And then every time you let! it will automatically be .ConfigureAwait(false)