Use mail box processor to process simple message

Category:
Mailbox Processor
Description: Use mail box processor to process simple message
Code:
type Agent<'T> = MailboxProcessor<'T>

let agentSample0() =
let agent =
Agent.Start(fun inbox ->
async { for i in [1..3] do
let! msg = inbox.Receive()
printfn "got message '%s'" msg } )
printfn "because the message is running on the background thread, please see the result at the console window behind"
agent.Post("msg1");
agent.Post("msg2");
agent.Post("msg3");

Execution Result:
because the message is running on the background thread, please see the result at the console window behind

Use mail box processor and isolation

Category:
Mailbox Processor
Description: Use mail box processor and isolation.
Code:
type Agent<'T> = MailboxProcessor<'T>

let agentSample1() =
let agent =
Agent.Start(fun inbox ->
async { let strings = Dictionary()
for i in [1..5] do
let! msg = inbox.Receive()
if strings.ContainsKey msg then
strings.[msg] <- strings.[msg] + 1
else
strings.[msg] <- 0
printfn "message '%s' now seen '%d' times" msg strings.[msg] } )
printfn "because the message is running on the background thread, please see the result at the console window behind"
agent.Post("msg1");
agent.Post("msg2");
agent.Post("msg3");
agent.Post("msg1");
agent.Post("msg2");
Execution Result:
because the message is running on the background thread, please see the result at the console window behind

Last edited Sep 12, 2011 at 11:38 PM by ttliu2000, version 1

Comments

No comments yet.