Metodo MailboxProcessor.PostAndTryAsyncReply<'Msg,'Reply> (F#)

Analogo a MailboxProcessor.AsyncPostAndReply, ma restituisce None se non viene ricevuta alcuna risposta entro il periodo di timeout.

Percorso di spazio dei nomi/modulo: Microsoft.FSharp.Control

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

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

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

Parametri

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

    Funzione utilizzata per incorporare AsyncReplyChannel nel messaggio da inviare.

  • timeout
    Tipo: int

    Parametro di timeout facoltativo (in millisecondi) per l'attesa di un messaggio di risposta.L'impostazione predefinita è -1 che corrisponde a Infinite.

Valore restituito

Calcolo asincrono (oggetto asincrono) che restituisce la risposta o None se il timeout scade.

Esempio

Nell'esempio di codice riportato di seguito viene illustrato come utilizzare il metodo 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

Di seguito viene riportata una sessione di esempio.

  
  
  
  
  
  
  
  

Piattaforme

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

Informazioni sulla versione

Versioni della libreria di base F#

Supportato in: 2,0, 4,0, portabile

Vedere anche

Riferimenti

Classe Control.MailboxProcessor<'Msg> (F#)

Spazio dei nomi Microsoft.FSharp.Control (F#)