MailboxProcessor.PostAndReply<'Msg,'Reply> 메서드(F#)
메시지를 에이전트에 게시하고 동기적으로 채널에서 회신을 기다립니다.
네임스페이스/모듈 경로: Microsoft.FSharp.Control
어셈블리: FSharp.Core(FSharp.Core.dll)
// Signature:
member this.PostAndReply : (AsyncReplyChannel<'Reply> -> 'Msg) * ?int -> 'Reply
// Usage:
mailboxProcessor.PostAndReply (buildMessage)
mailboxProcessor.PostAndReply (buildMessage, timeout = timeout)
매개 변수
buildMessage
형식: AsyncReplyChannel<'Reply> -> 'MsgAsyncReplyChannel을 보낼 메시지로 통합하는 함수입니다.
timeout
형식: int회신 메시지를 기다리는 선택적 시간 제한 매개 변수(밀리초)입니다.기본값은 Infinite에 해당하는 -1입니다.
반환 값
에이전트의 회신입니다.
설명
메시지는 메시지로 통합되도록 새 회신 채널에 buildMessage를 적용하여 생성됩니다.받는 에이전트는 이 메시지를 처리하고 이 회신 채널에서 Reply 메서드를 정확히 한 번만 호출해야 합니다.
예제
다음 코드 예제에서는 사서함 프로세서 에이전트를 시작하는 방법을 보여 줍니다.이 예제에서 콘솔에서 입력되는 각 줄은 메시지 대기열에 게시됩니다.프로그램이 각 메시지를 읽고 응답 채널을 사용하여 회신합니다.특수 메시지 "Stop"을 받으면 회신 중지가 보내지고 프로그램이 종료됩니다.
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
세션의 예를 들면 다음과 같습니다.
플랫폼
Windows Windows 서버 2012, Windows Server 2008 R2, Windows 7, 8
버전 정보
F# 코어 라이브러리 버전
지원: 2.0, 4.0, 노트북