Share via


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

Erstellt und startet einen Agent.

Namespace/Modulpfad: Microsoft.FSharp.Control

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

// Signature:
static member Start : (MailboxProcessor<'Msg> -> Async<unit>) * ?CancellationToken -> MailboxProcessor<'Msg>

// Usage:
MailboxProcessor.Start (body)
MailboxProcessor.Start (body, cancellationToken = cancellationToken)

Parameter

Rückgabewert

Der erstellte MailboxProcessor.

Hinweise

Die body-Funktion wird die asynchrone Berechnung generiert, die der Agent ausführt.

Beispiel

Das folgende Codebeispiel veranschaulicht, wie ein Postfachprozessor-Agent gestartet wird. In diesem Beispiel wird jede Eingabezeile in der Konsole an eine Nachrichtenwarteschlange gesendet. Das Programm liest jede Nachricht und antwortet mithilfe eines Antwortkanals. Wenn die Sondermeldung "Beenden" empfangen wird, wird die Antwort auf das Beenden gesendet und das Programm wird geschlossen.

open System

type Message = string * AsyncReplyChannel<string>

let formatString = "Message number {0} was received. Message contents: {1}"

let printThreadId note =

    // Append the thread ID.
    printfn "%d : %s" System.Threading.Thread.CurrentThread.ManagedThreadId note


let agent = MailboxProcessor<Message>.Start(fun inbox ->
    let rec loop n =
        async {
                let! (message, replyChannel) = inbox.Receive();
                printThreadId "MailboxProcessor"
                if (message = "Stop") then
                    replyChannel.Reply("Stopping.")
                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 rec loop() =
    printf "> "
    let input = Console.ReadLine()
    printThreadId("Console loop")
    let reply = agent.PostAndReply(fun replyChannel -> input, replyChannel)
    if (reply <> "Stopping.") then
        printfn "Reply: %s" reply
        loop()
    else
        ()
loop()

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

Beachten Sie folgende Sitzung.

                    

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.