// [snippet: Declarations] type Agent<'a> = MailboxProcessor<'a> let pong1 = Agent unit)>.Start(fun inbox -> let rec loop (state: System.Collections.Generic.List) = async { let! msg = inbox.Receive() match msg with // Build up state and return it at the end. | "Stop", _ -> for m in state do System.Console.WriteLine m | m, f -> state.Add m if state.Count > 5 then f "Bail!" for m in state do System.Console.WriteLine m else f "Pong" return! loop state } loop (new System.Collections.Generic.List()) ) let pong2 = Agent unit)>.Start(fun inbox -> let rec loop (state: System.Collections.Generic.List) = async { let! msg = inbox.Receive() match msg with // Build up state and return it at the end. | "Stop", _ -> for m in state do System.Console.WriteLine m | m, f -> state.Add m f "Pong" return! loop state } loop (new System.Collections.Generic.List()) ) let rec ping (target1: Agent<_>) (target2: Agent<_>) = Agent.Start(fun inbox -> async { let target = ref target1 for x=1 to 10 do (!target).Post("Ping", inbox.Post) let! msg = inbox.Receive() if msg = "Bail!" then target := target2 System.Console.WriteLine msg (!target).Post("Stop", inbox.Post) }) // [/snippet] // [snippet: Usage] ping pong1 pong2 // Pong // Pong // Pong // Pong // Pong // Ping // Ping // Ping // Ping // Ping // Ping // Bail! // Pong // Pong // Pong // Pong // Ping // Ping // Ping // Ping // [/snippet]