diff --git a/src/fsharp/FSharp.Core/mailbox.fs b/src/fsharp/FSharp.Core/mailbox.fs index 96782fa1e15..20b426cd0eb 100644 --- a/src/fsharp/FSharp.Core/mailbox.fs +++ b/src/fsharp/FSharp.Core/mailbox.fs @@ -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 @@ -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 @@ -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 diff --git a/tests/fsharp/core/controlMailbox/test.fsx b/tests/fsharp/core/controlMailbox/test.fsx index fc8a1cc7aea..31eed776a9b 100644 --- a/tests/fsharp/core/controlMailbox/test.fsx +++ b/tests/fsharp/core/controlMailbox/test.fsx @@ -66,6 +66,14 @@ module MailboxProcessorBasicTests = 100 + check + "c32398u6: MailboxProcessor null" + (let mb1 = new MailboxProcessor(fun inbox -> async { return () }) + mb1.StartImmediate(); + 100) + 100 + + check "c32398u7: MailboxProcessor Receive/PostAndReply" (let mb1 = new MailboxProcessor>(fun inbox -> async { let! msg = inbox.Receive()