我在消息循环中有一个超时异常(我真的打算设置一个超时),我试图按如下方式捕获它
let printerAgent = MailboxProcessor.Start(fun inbox->
// the message processing function
let rec messageLoop() = async{
try
// read a message
let! msg = inbox.Receive 30000
// process a message
match msg with
| Message text ->
sw.WriteLine("{0}: {1}", DateTime.UtcNow.ToShortTimeString(), text)
printfn "%s" text
// loop to top
return! messageLoop()
| Shutdown replyChannel ->
replyChannel.Reply()
// We do NOT do return! messageLoop() here
with
| exc ->
printfn "%s" exc.Message
}
// start the loop
messageLoop()
)
我可以看到控制台中打印的超时消息,但程序永远不会结束:我错过了什么?
这就是我printerAgent
在代码中调用的方式
printerAgent.PostAndReply( (fun replyChannel -> Shutdown replyChannel), 10000)
请注意,inbox.Receive()
它最终会在几分钟后正常终止,但我的目标是设置超时(例如 30 秒)。