Skip to content
Closed
Show file tree
Hide file tree
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
24 changes: 17 additions & 7 deletions src/fsharp/FSharp.Core/mailbox.fs
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ namespace Microsoft.FSharp.Control
member _.UnsafeMessageQueueContents = mailbox.UnsafeContents
#endif

member x.Start() =
member private x.PrepareStart() =
if started then
raise (new InvalidOperationException(SR.GetString(SR.mailboxProcessorAlreadyStarted)))
else
Expand All @@ -359,13 +359,18 @@ namespace Microsoft.FSharp.Control
// Protect the execution and send errors to the event.
// Note that exception stack traces are lost in this design - in an extended design
// the event could propagate an ExceptionDispatchInfo instead of an Exception.
let p =
async { try
do! body x
with exn ->
errorEvent.Trigger exn }
async { try
do! body x
with exn ->
errorEvent.Trigger exn }

member x.Start() =
let p = x.PrepareStart()
Async.Start(computation=p, cancellationToken=cancellationToken)

Async.Start(computation=p, cancellationToken=cancellationToken)
member x.StartImmediate() =
let p = x.PrepareStart()
Async.StartImmediate(computation=p, cancellationToken=cancellationToken)

member _.Post message = mailbox.Post message

Expand Down Expand Up @@ -438,3 +443,8 @@ namespace Microsoft.FSharp.Control
let mailboxProcessor = new MailboxProcessor<'Msg>(body, ?cancellationToken=cancellationToken)
mailboxProcessor.Start()
mailboxProcessor

static member StartImmediate(body, ?cancellationToken) =
let mailboxProcessor = new MailboxProcessor<'Msg>(body, ?cancellationToken=cancellationToken)
mailboxProcessor.StartImmediate()
mailboxProcessor
8 changes: 8 additions & 0 deletions tests/fsharp/core/controlMailbox/test.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ module MailboxProcessorBasicTests =
100


check
"c32398u6: MailboxProcessor null"
(let mb1 = new MailboxProcessor<int>(fun inbox -> async { return () })
mb1.StartImmediate();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need more testing than this - create a MailboxProcessor that does a side effect during it's start?

100)
100


check
"c32398u7: MailboxProcessor Receive/PostAndReply"
(let mb1 = new MailboxProcessor<AsyncReplyChannel<int>>(fun inbox -> async { let! msg = inbox.Receive()
Expand Down