Skip to content
This repository was archived by the owner on Nov 26, 2024. It is now read-only.
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 60 additions & 50 deletions docs/conceptual/control.mailboxprocessor['msg]-class-[fsharp].md
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,25 @@ A message-processing agent which executes an asynchronous computation.
[<Sealed>]
[<AutoSerializable(false)>]
type MailboxProcessor<'Msg> =
class
interface IDisposable
new MailboxProcessor : (MailboxProcessor<'Msg> -> Async<unit>) * ?CancellationToken -> MailboxProcessor<'Msg>
member this.Post : 'Msg -> unit
member this.PostAndAsyncReply : (AsyncReplyChannel<'Reply> -> 'Msg) * int option -> Async<'Reply>
member this.PostAndReply : (AsyncReplyChannel<'Reply> -> 'Msg) * int option -> 'Reply
member this.PostAndTryAsyncReply : (AsyncReplyChannel<'Reply> -> 'Msg) * ?int -> Async<'Reply option>
member this.Receive : ?int -> Async<'Msg>
member this.Scan : ('Msg -> Async<'T> option) * ?int -> Async<'T>
member this.Start : unit -> unit
static member Start : (MailboxProcessor<'Msg> -> Async<unit>) * ?CancellationToken -> MailboxProcessor<'Msg>
member this.TryPostAndReply : (AsyncReplyChannel<'Reply> -> 'Msg) * ?int -> 'Reply option
member this.TryReceive : ?int -> Async<'Msg option>
member this.TryScan : ('Msg -> Async<'T> option) * ?int -> Async<'T option>
member this.add_Error : Handler<Exception> -> unit
member this.CurrentQueueLength : int
member this.DefaultTimeout : int with get, set
member this.Error : IEvent<Exception>
member this.remove_Error : Handler<Exception> -> unit
class
interface IDisposable
new MailboxProcessor : (MailboxProcessor<'Msg> -> Async<unit>) * ?CancellationToken -> MailboxProcessor<'Msg>
member this.Post : 'Msg -> unit
member this.PostAndAsyncReply : (AsyncReplyChannel<'Reply> -> 'Msg) * int option -> Async<'Reply>
member this.PostAndReply : (AsyncReplyChannel<'Reply> -> 'Msg) * int option -> 'Reply
member this.PostAndTryAsyncReply : (AsyncReplyChannel<'Reply> -> 'Msg) * ?int -> Async<'Reply option>
member this.Receive : ?int -> Async<'Msg>
member this.Scan : ('Msg -> Async<'T> option) * ?int -> Async<'T>
member this.Start : unit -> unit
static member Start : (MailboxProcessor<'Msg> -> Async<unit>) * ?CancellationToken -> MailboxProcessor<'Msg>
member this.TryPostAndReply : (AsyncReplyChannel<'Reply> -> 'Msg) * ?int -> 'Reply option
member this.TryReceive : ?int -> Async<'Msg option>
member this.TryScan : ('Msg -> Async<'T> option) * ?int -> Async<'T option>
member this.add_Error : Handler<Exception> -> unit
member this.CurrentQueueLength : int
member this.DefaultTimeout : int with get, set
member this.Error : IEvent<Exception>
member this.remove_Error : Handler<Exception> -> unit
end
```

Expand Down Expand Up @@ -90,40 +90,50 @@ This type is named **FSharpMailboxProcessor** in the .NET assembly. If accessing
|[Start](https://msdn.microsoft.com/library/ebf18bf3-ba17-42b9-91ac-313a7eee6fa0)|Creates and starts an agent. The **body** function is used to generate the asynchronous computation executed by the agent.|
**The following example shows the basic use of the MailboxProcessor class.**
[!code-fsharp[Main](snippets/fsmailboxprocessor/snippet2.fs)]

**Sample Output**
**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.****The following example shows how to use MailboxProcessor to create a simple agent that accepts various types of messages and returns appropriate replies. This server agent represents a market maker, which is a buying and selling agent on a stock exchange that sets bid and ask prices for assets. Clients can query for prices, or buy and sell shares.**

```text
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.
```

The following example shows how to use MailboxProcessor to create a simple agent that accepts various types of messages and returns appropriate replies. This server agent represents a market maker, which is a buying and selling agent on a stock exchange that sets bid and ask prices for assets. Clients can query for prices, or buy and sell shares.**
[!code-fsharp[Main](snippets/fsmailboxprocessor/snippet3.fs)]
**Sample Output**
**Posting message for AAA**
**Replying with Info for AAA**
**Posting message for BBB**
**Replying with Notification:**
**Bought 100 units of BBB at price $20.100000. Total purchase $2010.000000.**
**Marketmaker balance: $ 2010.00**
**Posting message for CCC**
**Replying with Notification:**
**Sold 100 units of CCC at price $30.000000. Total sale $3000.000000.**
**Marketmaker balance: $ -990.00**
**Posting message for WrongCode**
**Posting message with large number of shares of AAA.**
**Insufficient shares to fulfill order for 1000000000 units of AAA.**
**Posting message with too large a monetary amount.**
**Insufficient cash to fulfill order for 100000000 units of AAA.**
**Press any key...**
**Posting BUY CCC 1338.**
**Replying with Notification:**
**Bought 1338 units of CCC at price $30.150000. Total purchase $40340.700000.**
**Marketmaker balance: $ 39350.70**
**Program+Snippet3+Reply+Notify**
**Posting BUY BBB 1961.**
**Replying with Notification:**
**Bought 1961 units of BBB at price $20.100000. Total purchase $39416.100000.**
**Marketmaker balance: $ 78766.80**

```text
Posting message for AAA
Replying with Info for AAA
Posting message for BBB
Replying with Notification:
Bought 100 units of BBB at price $20.100000. Total purchase $2010.000000.
Marketmaker balance: $ 2010.00
Posting message for CCC
Replying with Notification:
Sold 100 units of CCC at price $30.000000. Total sale $3000.000000.
Marketmaker balance: $ -990.00
Posting message for WrongCode
Posting message with large number of shares of AAA.
Insufficient shares to fulfill order for 1000000000 units of AAA.
Posting message with too large a monetary amount.
Insufficient cash to fulfill order for 100000000 units of AAA.
Press any key...
Posting BUY CCC 1338.
Replying with Notification:
Bought 1338 units of CCC at price $30.150000. Total purchase $40340.700000.
Marketmaker balance: $ 39350.70
Program+Snippet3+Reply+Notify
Posting BUY BBB 1961.
Replying with Notification:
Bought 1961 units of BBB at price $20.100000. Total purchase $39416.100000.
Marketmaker balance: $ 78766.80
```

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

Expand Down