Skip to content

Instantly share code, notes, and snippets.

@isaacabraham
Created August 21, 2013 13:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isaacabraham/6294739 to your computer and use it in GitHub Desktop.
Save isaacabraham/6294739 to your computer and use it in GitHub Desktop.
let createAgent() =
let tokenSource = new System.Threading.CancellationTokenSource()
Agent.Start((fun inbox -> async {
printfn "spun up a new agent..."
while true do
let! data = inbox.Receive()
let! result = // Do some async work here...
match result with
// Everything done, we can cancel
| AllDone -> printfn "completed!"
tokenSource.Cancel()
// More to do here, wait a bit and then try again
| MoreToDo -> printfn "retrying in a bit..."
do! Async.Sleep 2500
inbox.Post data }), tokenSource.Token)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment