MailboxProcessor.TryPostAndReply<'Msg,'Reply> 메서드(F#)

업데이트: 2011년 1월

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> -> 'Msg

    AsyncReplyChannel을 보낼 메시지로 통합하는 함수입니다.

  • timeout
    형식: int

    회신 메시지를 기다리는 선택적 시간 제한 매개 변수(밀리초)입니다. 기본값은 Infinite()에 해당하는 -1입니다.

반환 값

에이전트로부터의 회신 또는 시간 제한이 만료되는 경우 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 7, Windows Vista SP2, Windows XP SP3, Windows XP x64 SP2, Windows Server 2008 R2, Windows Server 2008 SP2, Windows Server 2003 SP2

버전 정보

F# 런타임

지원되는 버전: 2.0, 4.0

Silverlight

지원되는 버전: 3

참고 항목

참조

Control.MailboxProcessor<'Msg> 클래스(F#)

Microsoft.FSharp.Control 네임스페이스(F#)

변경 기록

날짜

변경 내용

이유

2011년 1월

코드 예제를 추가했습니다.

향상된 기능 관련 정보