MailboxProcessor.PostAndReply<'Msg,'Reply> Method (F#)
Posts a message to an agent and await a reply on the channel, synchronously.
Namespace/Module Path: Microsoft.FSharp.Control
Assembly: FSharp.Core (in FSharp.Core.dll)
// Signature:
member this.PostAndReply : (AsyncReplyChannel<'Reply> -> 'Msg) * ?int -> 'Reply
// Usage:
mailboxProcessor.PostAndReply (buildMessage)
mailboxProcessor.PostAndReply (buildMessage, timeout = timeout)
Parameters
buildMessage
Type: AsyncReplyChannel<'Reply> -> 'MsgThe function to incorporate the AsyncReplyChannel into the message to be sent.
timeout
Type: intAn optional timeout parameter (in milliseconds) to wait for a reply message. The default is -1 which corresponds to Infinite.
Return Value
The reply from the agent.
Remarks
The message is generated by applying buildMessage to a new reply channel to be incorporated into the message. The receiving agent must process this message and invoke the Reply method on this reply channel precisely once.
Example
The following code example shows how to start a mailbox processor agent. In this example, each line of input from the console is posted to a message queue. The program reads each message and replies by using a reply channel. When the special message "Stop" is received, the Stop reply is sent and the program exits.
open System
type Message = string * AsyncReplyChannel<string>
let formatString = "Message number {0} was received. Message contents: {1}"
let printThreadId note =
// Append the thread ID.
printfn "%d : %s" System.Threading.Thread.CurrentThread.ManagedThreadId note
let agent = MailboxProcessor<Message>.Start(fun inbox ->
let rec loop n =
async {
let! (message, replyChannel) = inbox.Receive();
printThreadId "MailboxProcessor"
if (message = "Stop") then
replyChannel.Reply("Stopping.")
else
replyChannel.Reply(String.Format(formatString, n, message))
do! loop (n + 1)
}
loop 0)
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() =
printf "> "
let input = Console.ReadLine()
printThreadId("Console loop")
let reply = agent.PostAndReply(fun replyChannel -> input, replyChannel)
if (reply <> "Stopping.") then
printfn "Reply: %s" reply
loop()
else
()
loop()
printfn "Press Enter to continue."
Console.ReadLine() |> ignore
Following is an example session.
Mailbox Processor Test Type some text and press Enter to submit a message. Type 'Stop' to close the program. > hello 1 : Console loop 4 : mailboxProcessor Reply: Message number 0 was received. Message contents: hello > testing 1 : Console loop 3 : mailboxProcessor Reply: Message number 1 was received. Message contents: testing > hello? 1 : Console loop 4 : mailboxProcessor Reply: Message number 2 was received. Message contents: hello? > testing 1 2 3 1 : Console loop 3 : mailboxProcessor Reply: Message number 3 was received. Message contents: testing 1 2 3 > Stop 1 : Console loop 4 : mailboxProcessor Press Enter to continue.
Platforms
Windows 8, Windows 7, Windows Server 2012, Windows Server 2008 R2
Version Information
F# Core Library Versions
Supported in: 2.0, 4.0, Portable