Skip to content

Instantly share code, notes, and snippets.

@IvanRainbolt
Last active October 3, 2020 02:10
Show Gist options
  • Save IvanRainbolt/e8689e74eb1579ca6205985b6aec92a9 to your computer and use it in GitHub Desktop.
Save IvanRainbolt/e8689e74eb1579ca6205985b6aec92a9 to your computer and use it in GitHub Desktop.
Simple Background task running
namespace UI.FLib
module Loop =
// Loop that keeps running forever until an exception happens
let rec loop () = async {
do! Async.Sleep(5000)
printfn "Working"
return! loop () }
// Call the loop in a try .. finally block to run some cleanup code at the end
let main () = async {
try
do! loop ()
finally
printfn "That's it!" }
// Start the work in the background and create a cancellation token
let cts = new System.Threading.CancellationTokenSource()
//Async.Start(main (), cts.Token)
// To cancel it, just call: cts.Cancel()
let BackgroundTimerStart () =
Async.Start(main (), cts.Token)
// To cancel it, just call: cts.Cancel()
let BackgroundTimerEnd () =
cts.Cancel()
// To cancel it, just call: cts.Cancel()
//source: https://stackoverflow.com/questions/26706149/f-continuous-loop-in-f/26706752
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment