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

MailboxProcessor.Error<'Msg> Property (F#)

Den Delimarsky|Last Updated: 7/6/2016
|
1 Contributor

Occurs when the execution of the agent results in an exception.

Namespace/Module Path: Microsoft.FSharp.Control

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

Syntax

// Signature:
member this.Error :  [IEvent](https://msdn.microsoft.com/library/7976554f-9aa8-451f-a69d-d4670c064432)<Exception>

// Usage:
mailboxProcessor.Error

Return Value

The error event as an object that implements IEvent

Example

The following code shows how to use the Error event to handle an exception that occurs in the body of the agent.

open System

type Message = string

let agent = MailboxProcessor<Message>.Start(fun inbox ->
    let rec loop n =
        async {
                let! message = inbox.Receive(10000);
                printfn "Message number %d. Message contents: %s" n message
                do! loop (n + 1)
        }
    loop 0)

agent.Error.Add(fun exn ->
    match exn with
    | :? System.TimeoutException as exn -> printfn "The agent timed out."
                                           printfn "Press Enter to close the program."
                                           Console.ReadLine() |> ignore
                                           exit(1)
    | _ -> printfn "Unknown exception.")

printfn "Mailbox Processor Test"
printfn "Type some text and press Enter to submit a message."
  
while true do
    Console.ReadLine() |> agent.Post

An example session follows.

Mailbox Processor Test
Type some text and press Enter to submit a message.
hello
Message number 0. Message contents: hello
testing
Message number 1. Message contents: testing
The agent timed out.
Press Enter to close the program.

Platforms

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#)

© 2016 Microsoft