Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ConfigureAwait(false) in F#

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/

like image 701
dustinmoris Avatar asked Jun 30 '16 17:06

dustinmoris


1 Answers

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 computational 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)

like image 162
jbtule Avatar answered Oct 06 '22 08:10

jbtule