更新:2011 年 1 月
在代理的执行导致异常时发生。
命名空间/模块路径: Microsoft.FSharp.Control
程序集:FSharp.Core(在 FSharp.Core.dll 中)
// Signature:
member this.Error : IEvent<Exception>
// Usage:
mailboxProcessor.Error
返回值
错误事件是一个对象,它能实现 事件。
示例
以下代码演示如何使用 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 7、Windows Vista SP2、Windows XP SP3、Windows XP x64 SP2、Windows Server 2008 R2、Windows Server 2008 SP2、Windows Server 2003 SP2
版本信息
F# 运行时
受以下版本支持:2.0、4.0
Silverlight
受以下版本支持:3
请参见
参考
Control.MailboxProcessor<'Msg> 类 (F#)
Microsoft.FSharp.Control 命名空间 (F#)
修订记录
Date |
修订记录 |
原因 |
---|---|---|
2011 年 1 月 |
添加了代码示例。 |
信息补充。 |