Skip to content

Instantly share code, notes, and snippets.

@OnurGumus
Created July 24, 2024 08:59
Show Gist options
  • Save OnurGumus/26939f21190262d2b21c9ba9d899d7b1 to your computer and use it in GitHub Desktop.
Save OnurGumus/26939f21190262d2b21c9ba9d899d7b1 to your computer and use it in GitHub Desktop.
Fable Async
open System.Threading
let taskGrandchild(order: int) : Async<unit> =
async {
printfn $"Starting Grandchild({order})"
let! ct = Async.CancellationToken
printfn $"Cancellation token GrandChild({order}): {ct.GetHashCode()}"
// use! c = Async.OnCancel(fun () -> printfn $"Cancelled Task Grandchild: {order}")
while (true) do
printfn $"Waiting Grandchild({order})"
do! Async.Sleep(100)
}
let taskChild() : Async<unit> =
async {
printfn "Starting Child"
let! ct = Async.CancellationToken
printfn $"Cancellation token child: {ct.GetHashCode()}"
//use! c = Async.OnCancel(fun () -> printfn "Cancelled Task Child")
while (true) do
do! taskGrandchild(1)
do! taskGrandchild(2)
}
let task () : Async<unit> =
async {
printfn "Starting"
let! ct = Async.CancellationToken
printfn $"Cancellation token: {ct.GetHashCode()}"
//use! c = Async.OnCancel(fun () -> printfn "Cancelled")
do! taskChild()
}
let cts = new CancellationTokenSource(1000)
let ct = cts.Token
printfn $"Cancellation token main: {ct.GetHashCode()}"
Async.StartWithContinuations(task (), ignore, ignore, ignore, cts.Token)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment