Udostępnij za pośrednictwem


MailboxProcessor.PostAndTryAsyncReply < "Msg," odpowiedzi > Metoda (F#)

Jak MailboxProcessor.AsyncPostAndReply, ale zwraca None , jeśli nie odpowiedzi przed upływem limitu czasu.

Ścieżka obszaru nazw/modułu: Microsoft.FSharp.Control

Zestaw: FSharp.Core (w FSharp.Core.dll)

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

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

Parametry

  • buildMessage
    Typ: AsyncReplyChannel<'Reply> -> 'Msg

    Włączenie funkcji AsyncReplyChannel na wysłanie wiadomości.

  • timeout
    Typ: int

    Parametr opcjonalny limit czasu (w milisekundach) oczekiwania na odpowiedź.Wartością domyślną jest -1, która odpowiada Infinite.

Wartość zwracana

Asynchroniczne obliczeń (Async obiektu), zwróci odpowiedź lub None Jeśli minie.

Przykład

Poniższy kod pokazuje, jak używać PostAndTryAsyncReply metody.

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

Następujące próbki sesji.

  
  
  
  
  
  
  
  

Platformy

Windows 8, Windows 7, Windows Server 2012 Windows Server 2008 R2

Informacje o wersji

F# Core wersji biblioteki

Obsługiwane: 2.0, 4.0, przenośne

Zobacz też

Informacje

Control.MailboxProcessor < "Msg > Klasa (F#)

Obszar nazw Microsoft.FSharp.Control (F#)