Share via


MailboxProcessor.PostAndAsyncReply<'Msg,'Reply>-Methode (F#)

Sendet asynchron eine Meldung an einen Agent und wartet auf eine Antwort über den Kanal.

Namespace/Modulpfad: Microsoft.FSharp.Control

Assembly: FSharp.Core (in FSharp.Core.dll)

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

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

Parameter

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

    Die Funktion, mit der AsyncReplyChannel in die zu sendende Meldung eingefügt werden soll.

  • timeout
    Typ: int

    Ein optionaler Timeoutparameter (in Millisekunden), der die Wartezeit für eine Antwortmeldung angibt.Der Standardwert ist -1. Dies entspricht Infinite.

Rückgabewert

Eine asynchrone Berechnung (Async-Objekt), die auf die Antwort des Agents wartet.

Hinweise

Die Meldung wird generiert, indem buildMessage auf einen neuen Antwortkanal angewendet wird, der in die Meldung integriert werden soll.Der empfangende Agent muss diese Meldung verarbeiten und die Reply-Methode genau einmal für diesen Antwortkanal aufrufen.

Beispiel

Im folgenden Codebeispiel ist ein Postfachprozessor-Agent dargestellt, der PostAndAsyncReply verwendet.Der Rückgabewert von PostAndAsyncReply ist ein asynchroner Workflow, der in diesem Beispiel gestartet wird, indem Sie Async.StartWithContinuations verwenden, um den Code einzurichten, der die Antwort behandelt.

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();
                // Delay so that the responses come in a different order.
                do! Async.Sleep( 5000 - 1000 * n);
                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."

let isCompleted = false
while (not isCompleted) do
    printf "> "
    let input = Console.ReadLine()
    let messageAsync = agent.PostAndAsyncReply(fun replyChannel -> input, replyChannel)

    // Set up a continuation function (the first argument below) that prints the reply.
    // The second argument is the exception continuation (not used).
    // The third argument is the cancellation continuation (not used).
    Async.StartWithContinuations(messageAsync, 
         (fun reply -> printfn "%s" reply),
         (fun _ -> ()),
         (fun _ -> ()))

printfn "Press Enter to continue."
Console.ReadLine() |> ignore

Beachten Sie folgende Sitzung.Die Ausgabe überlappt sich, was angibt, dass die Nachrichtenverarbeitungsfunktion in mehreren Threads ausgeführt wird.

  
  
  
  
  
  
  
  
  
  

Plattformen

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

Versionsinformationen

F#-Kern-Bibliotheks-Versionen

Unterstützt in: 2,0, 4,0, portablen

Siehe auch

Referenz

Control.MailboxProcessor<'Msg>-Klasse (F#)

Microsoft.FSharp.Control-Namespace (F#)