Table of contents
Collapse the table of content
Expand the table of content

MailboxProcessor.Post<'Msg> Method (F#)

Den Delimarsky|Last Updated: 8/18/2017
1 Contributor

Posts a message to the message queue of the MailboxProcessor, asynchronously.

Namespace/Module Path: Microsoft.FSharp.Control

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


// Signature:
member this.Post : 'Msg -> unit

// Usage:
mailboxProcessor.Post (message)


message Type: 'Msg

The message to post.


The following code example shows how to start a mailbox processor agent and post messages to it.

open System
open Microsoft.FSharp.Control

type Message(id, contents) =
    static let mutable count = 0
    member this.ID = id
    member this.Contents = contents
    static member CreateMessage(contents) =
        count <- count + 1
        Message(count, contents)

let mailbox = new MailboxProcessor<Message>(fun inbox ->
    let rec loop count =
        async { printfn "Message count = %d. Waiting for next message." count
                let! msg = inbox.Receive()
                printfn "Message received. ID: %d Contents: %s" msg.ID msg.Contents
                return! loop( count + 1) }
    loop 0)



Console.WriteLine("Press any key...")
Console.ReadLine() |> ignore

Following is an example session.

Press any key...
Message count = 0. Waiting for next message.
Message received. ID: 1 Contents: ABC
Message count = 1. Waiting for next message.
Message received. ID: 2 Contents: XYZ
Message count = 2. Waiting for next message.


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

Version Information

F# Core Library Versions

Supported in: 2.0, 4.0, Portable

See Also

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

Microsoft.FSharp.Control Namespace (F#)

© 2020 Microsoft