共用方式為


MailboxProcessor.Error<'Msg> 屬性 (F#)

執行代理程式而造成例外狀況時發生。

**命名空間/模組路徑:**Microsoft.FSharp.Control

組件:FSharp.Core (在 FSharp.Core.dll 中)

// Signature:
member this.Error :  IEvent<Exception>

// Usage:
mailboxProcessor.Error

傳回值

錯誤事件,其型別為實作 IEvent 的物件

範例

下列程式碼示範如何使用 Error 事件以處理在代理程式主體中發生的例外狀況。

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

接下來是工作階段範例。

  
  
  
  
  

平台

Windows 8 中, Windows 7, Windows Server 2012 上, Windows Server 2008 R2

版本資訊

F# 核心程式庫版本

支援版本:2.0, 4.0,可攜式執行檔 (PE)。

請參閱

參考

Control.MailboxProcessor<'Msg> 類別 (F#)

Microsoft.FSharp.Control 命名空間 (F#)