asynchronous - Catch exception from async workflow run on different thread -
let failing = async { failwith "foo" } let test () = try async.start(failing) | exn -> printf "caught"
this code doesn't catch exception. how can start asynchronous workflow on separate thread , catch exception in main program?
as alternative start workflow task , use it's methods , properties instead. example task.result
rethrow exception again works, , tried:
let test () = try async.startastask failing |> fun t -> t.result _ -> printfn "caught"
run
> test ();; caught val : unit = ()
on differnt thread
sorry - saw want on different thread - in case want use internal approach rch gave - use continuewith
(although bit ugly):
open system.threading.tasks let test () = (async.startastask failing).continuewith(fun (t : task<_>) -> try t.result _ -> printfn "caught")
run
> test ();; caught val : task = system.threading.tasks.task {asyncstate = null; creationoptions = none; exception = null; id = 3; iscanceled = false; iscompleted = true; isfaulted = false; status = rantocompletion;}
without async.catch
also don't need async.catch
:
let test () = async { try do! failing _ -> printfn "caught" } |> async.start
Comments
Post a Comment