c# - Async.RunSynchronously() vs Async.StartAsTask().Result -
lets had f# computation deal in c# , wanted make them run synchronously. difference underneath hood between:
public static t runsync<t>(fsharpasync<t> computation) { return fsharpasync.runsynchronously(computation, timeout: fsharpoption<int>.none, cancellationtoken: fsharpoption<system.threading.cancellationtoken>.none ); }
or
public static t runsync<t>(fsharpasync<t> computation) { return fsharpasync.startastask(computation, taskcreationoptions: fsharpoption<taskcreationoptions>.none, cancellationtoken: fsharpoption<system.threading.cancellationtoken>.none ).result; }
sorry if seems simple question, quite new async programming!
i think same - reed says, first 1 queues work directly while other 1 creates task
, created task
lightweight object (under cover, same , task created using taskcompletionsource
- can see code here).
if in control of f# library, recommendation expose method returns task
, hide f# async - make usage c# more convenient , avoid special f# types (like options). if have somework
function in f#, write:
type nicecsharp = static member somework() = async.startastask(somework()) static member somework(cancellationtoken) = async.startastask(somework(), cancellationtoken = cancellationtoken)
on c# side, see nice overloaded method returns task
, works await
. of course, can await result synchronously using result
, overhead not big - , fact you're exposing nice c# api make easier integrate f# in system.
Comments
Post a Comment