MailboxProcessor.PostAndAsyncReply<'Msg,'Reply> メソッド (F#)

更新 : 2011 年 1 月

エージェントにメッセージをポストし、チャネルの応答を非同期的に待機します。

名前空間/モジュール パス: Microsoft.FSharp.Control

アセンブリ: FSharp.Core (FSharp.Core.dll 内)

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

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

パラメーター

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

    送信されるメッセージに AsyncReplyChannel を組み込む関数。

  • timeout
    型: int

    応答メッセージを待機するオプションのタイムアウト パラメーター (ミリ秒単位)。 既定値は Infinite() に対応する -1 です。

戻り値

エージェントからの応答を待機する非同期計算 (Async オブジェクト)。

解説

メッセージは、メッセージに結合される新しい応答チャネルに buildMessage を適用することで生成されます。 受信するエージェントは、このメッセージを処理し、この応答チャネル上の Reply メソッドを正確に 1 回呼び出す必要があります。

使用例

PostAndAsyncReply を使用するメールボックス プロセッサ エージェントのコード例を次に示します。 PostAndAsyncReply の戻り値は、応答を処理するコードを設定するための非同期ワークフローで、この例では、Async.StartWithContinuations を使用して開始されています。

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

セッション例を次に示します。 出力はインターリーブされる場合があり、メッセージ処理関数が複数のスレッドで実行されていることを示します。

                      

プラットフォーム

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

バージョン情報

F# ランタイム

サポート対象: 2.0、4.0

Silverlight

サポート: 3

参照

その他の技術情報

Control.MailboxProcessor<'Msg> クラス (F#)

Microsoft.FSharp.Control 名前空間 (F#)

履歴の変更

日付

履歴

理由

2011 年 1 月

コード例を追加。

情報の拡充