MailboxProcessor.TryPostAndReply<'Msg,'Reply> 메서드(F#)
MailboxProcessor.PostAndReply와 비슷하지만, 시간 제한 기간 내에 회신이 없으면 None을 반환합니다.
네임스페이스/모듈 경로: Microsoft.FSharp.Control
어셈블리: FSharp.Core(FSharp.Core.dll)
// Signature:
member this.TryPostAndReply : (AsyncReplyChannel<'Reply> -> 'Msg) * ?int -> 'Reply option
// Usage:
mailboxProcessor.TryPostAndReply (buildMessage)
mailboxProcessor.TryPostAndReply (buildMessage, timeout = timeout)
매개 변수
buildMessage
형식: AsyncReplyChannel<'Reply> -> 'MsgAsyncReplyChannel을 보낼 메시지로 통합하는 함수입니다.
timeout
형식: int회신 메시지를 기다리는 선택적 시간 제한 매개 변수(밀리초)입니다. -1에 해당하는 기본값은 Infinite입니다.
반환 값
에이전트로부터의 회신 또는 시간 제한이 만료되는 경우 None입니다.
예제
다음 예제에서는 TryPostAndReply의 사용 방법을 보여 줍니다. 이 예에서는 에이전트에 지연이 발생한 결과 시간이 초과됩니다.
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("Stop")
else
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
loop()
else
()),
(fun exn -> ()),
(fun _ -> ()))
loop()
printfn "Press Enter to continue."
Console.ReadLine() |> ignore
다음 세션의 예를 참조하십시오.
플랫폼
Windows 8, Windows 7, Windows Server 2012, Windows Server 2008 R2
버전 정보
F# Core 라이브러리 버전
2.0, 4.0, 노트북 지원