Skip to content
This repository
tree: 81acd122d4
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 40 lines (33 sloc) 1.347 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
// ----------------------------------------------------------------------------
// F# async extensions (AutoCancel.fsx)
// (c) Tomas Petricek, 2011, Available under Apache 2.0 license.
// ----------------------------------------------------------------------------

// This example demonstrates how to use 'AutoCancelAgent'
// The agent automatically stops its body when disposed.

#r @"..\build\FSharpx.Core.dll"
open FSharp.Control

let op = async {
  // Create a local agent that is disposed when the
  // workflow completes (using the 'use' construct)
  use agent = AutoCancelAgent.Start(fun agent -> async {
    try
      while true do
        // Wait for a message - note that we use timeout
        // to allow cancellation (when the operation completes)
        let! msg = agent.Receive(1000)
        match msg with
        | (n, reply:AsyncReplyChannel<unit>) ->
            // Print number and reply to the sender
            printfn "%d" n
            reply.Reply(())
        | _ -> ()
    finally
      // Called when the agent is disposed
      printfn "agent completed" })
  
  // Do some processing using the agent...
  for i in 0 .. 10 do
    do! Async.Sleep(100)
    do! agent.PostAndAsyncReply(fun r -> i, r)

  do! Async.Sleep(100)
  printfn "workflow completed" }

Async.Start(op)
Something went wrong with that request. Please try again.