Share via


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

MailboxProcessor.AsyncPostAndReply와 비슷하지만, 시간 제한 기간 내에 회신이 없으면 None을 반환합니다.

네임스페이스/모듈 경로: Microsoft.FSharp.Control

어셈블리: FSharp.Core(FSharp.Core.dll)

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

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

매개 변수

  • buildMessage
    형식: AsyncReplyChannel<'Reply> -> 'Msg

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

  • timeout
    형식: int

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

반환 값

회신을 반환하는 비동기 계산(Async 개체)이거나 제한 시간이 만료된 경우 None입니다.

예제

다음 코드는 PostAndTryAsyncReply 메서드를 사용하는 방법을 보여 줍니다.

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 triggers 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))

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

let mutable isCompleted = false 
while (not isCompleted) do
    printf "> " 
    let input = Console.ReadLine()
    let messageAsync = agent.PostAndTryAsyncReply((fun replyChannel -> input, replyChannel), 1000)
    // Set up a continuation function (the first argument below) that prints the reply. 
    // The second argument is the exception continuation. 
    // The third argument is the cancellation continuation (not used).
    Async.StartWithContinuations(messageAsync, 
         (fun reply -> 
             match reply with
             | None -> printfn "Reply timeout exceeded."
             | Some reply -> if (reply = "Stop") then
                                 isCompleted <- true 
                             else printfn "%s" reply),
         (fun exn ->
            printfn "Exception occurred: %s" exn.Message),
         (fun _ -> ()))

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, 노트북 지원

참고 항목

참조

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

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