Table of contents
Collapse the table of content
Expand the table of content

MailboxProcessor.TryPostAndReply<'Msg,'Reply> Method (F#)

Den Delimarsky|Last Updated: 8/18/2017
1 Contributor

Like MailboxProcessor.PostAndReply, but returns None if there is no reply within the timeout period.

Namespace/Module Path: Microsoft.FSharp.Control

Assembly: FSharp.Core (in FSharp.Core.dll)


// Signature:
member this.TryPostAndReply : (AsyncReplyChannel<'Reply> -> 'Msg) * ?int -> 'Reply option

// Usage:
mailboxProcessor.TryPostAndReply (buildMessage)
mailboxProcessor.TryPostAndReply (buildMessage, timeout = timeout)


buildMessage Type: AsyncReplyChannel<'Reply> -> 'Msg

The function to incorporate the AsyncReplyChannel into the message to be sent.

timeout Type: int

An optional timeout parameter (in milliseconds) to wait for a reply message. Defaults to -1 which corresponds to System.Threading.Timeout.Infinite.

Return Value

The reply from the agent or None if the timeout expires.


The following example shows how to use TryPostAndReply. In this example, the agent has a delay that eventually results in a timeout.

open System

type Message = string * AsyncReplyChannel<string>

let formatString = "Message number {0} was received. Message contents: {1}"

let agent = MailboxProcessor<Message>.Start(fun inbox ->
    let rec loop n =
        async {
                let! (message, replyChannel) = inbox.Receive();
                // The delay gets longer with each message, and eventually will trigger a timeout.
                do! Async.Sleep(200 * n );
                if (message = "Stop") then
                    replyChannel.Reply(String.Format(formatString, n, message))
                do! loop (n + 1)
    loop 0)

let asyncReadInput =
   async {
       printf "> "
       let input = Console.ReadLine();
       return input

printfn "Mailbox Processor Test"
printfn "Type some text and press Enter to submit a message."
printfn "Type 'Stop' to close the program."

let rec loop() =
    Async.StartWithContinuations(asyncReadInput, (fun input ->
        let reply = agent.TryPostAndReply((fun replyChannel -> input, replyChannel), 1000)
        match reply with
        | None -> printfn "Timeout exceeded."
        | Some(reply) ->
            if (reply <> "Stop") then
                printfn "Reply: %s" reply
        (fun exn -> ()),
        (fun _ -> ()))

printfn "Press Enter to continue."
Console.ReadLine() |> ignore

A sample session follows.

Mailbox Processor Test
Type some text and press Enter to submit a message.
Type 'Stop' to close the program.
> test1Reply: Message number 0 was received. Message contents: test1
> test2Reply: Message number 1 was received. Message contents: test2
> test3Reply: Message number 2 was received. Message contents: test3
> test4Reply: Message number 3 was received. Message contents: test4
> test5Reply: Message number 4 was received. Message contents: test5
> test6
Timeout exceeded.
Press Enter to continue.


Windows 8, Windows 7, Windows Server 2012, Windows Server 2008 R2

Version Information

F# Core Library Versions

Supported in: 2.0, 4.0, Portable

See Also

Control.MailboxProcessor<'Msg> Class (F#)

Microsoft.FSharp.Control Namespace (F#)

© 2019 Microsoft