Share via


MailboxProcessor.Receive<'Msg>-Methode (F#)

Wartet auf eine Meldung. Die erste Meldung in der Reihenfolge des Eintreffens wird verarbeitet.

Namespace/Modulpfad: Microsoft.FSharp.Control

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

// Signature:
member this.Receive : ?int -> Async<'Msg>

// Usage:
mailboxProcessor.Receive ()
mailboxProcessor.Receive (timeout = timeout)

Parameter

  • timeout
    Typ: int

    Ein optionales Timeout in Millisekunden. Wird standardmäßig auf -1 festgelegt, was Infinite() entspricht.

Ausnahmen

Ausnahme

Bedingung

TimeoutException

Wird ausgelöst, wenn das Timeout überschritten wird.

Rückgabewert

Eine asynchrone Berechnung (Async-Objekt), die die empfangene Meldung zurückgibt.

Hinweise

Diese Methode ist für die Verwendung im Coderumpf des Agents vorgesehen. Für jeden Agent darf maximal ein Reader gleichzeitig aktiv sein. Es darf also nur maximal ein Aufruf von Receive, ee370578(v=vs.100).md, Scan oder TryScan aktiv sein.

Beispiel

Im folgenden Beispiel wird die Verwendung der Receive-Methode gezeigt. In diesem Fall wird ein Timeout von 10 Sekunden angegeben. Im Allgemeinen wird die Nachrichtenverarbeitungsfunktion auf einem anderen Thread aus der Post-Funktion ausgeführt, sodass Sie die Timeoutausnahme in der Nachrichtenverarbeitungsfunktion abfangen müssen. In diesem Beispiel verursacht die Timeoutausnahme die Fortsetzung der Schleife und erhöht die Meldungsnummer um 1.

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 {            
            try
                let! (message, replyChannel) = inbox.Receive(10000);

                if (message = "Stop") then
                    replyChannel.Reply("Stop")
                else
                    replyChannel.Reply(String.Format(formatString, n, message))
                do! loop (n + 1)

            with
            | :? TimeoutException -> 
                printfn "The mailbox processor timed out."
                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 rec loop() =
    printf "> "
    let input = Console.ReadLine()
    let reply = agent.PostAndReply(fun replyChannel -> input, replyChannel)
    if (reply <> "Stop") then
        printfn "Reply: %s" reply
        loop()
    else
        ()
loop()

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

Eine typische Sitzung folgt. Beachten Sie, dass die Meldung 2 aufgrund des Timeouts übersprungen wird.

                    

Plattformen

Windows 7, Windows Vista SP2, Windows XP SP3, Windows XP x64 SP2, Windows Server 2008 R2, Windows Server 2008 SP2, Windows Server 2003 SP2

Versionsinformationen

F#-Runtime

Unterstützt in: 2.0, 4.0

Silverlight

Unterstützt in: 3

Siehe auch

Weitere Ressourcen

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

Microsoft.FSharp.Control-Namespace (F#)

Änderungsprotokoll

Datum

Versionsgeschichte

Grund

Januar 2011

Codebeispiel hinzugefügt.

Informationsergänzung.