[英]mailboxprocessor does not terminate
我在消息循环中有一个超时异常(我真的打算设置一个超时),我试图按如下方式捕获它
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 秒)。
我想我看到了概念上的问题。 在收到最终关闭消息之前,我无法终止消息循环(否则程序发送的所有以下printerAgent.Post
消息将在队列中保持未处理状态,不会因任何错误而阻塞程序和最终关闭消息,由打印机代理发送的printerAgent.PostAndReply
将超时,也不会因任何错误而阻塞程序)。 我应该返回消息循环,以便它在超时后实际上可以继续,就像正常接收到的消息一样:
with
| exc ->
printfn "%s" exc.Message
return! messageLoop() // important! I guess I can't really terminate the message loop from here
此时程序同时终止:我只看到控制台中打印了很多Timeout of Mailbox.Receive
(在消息循环空闲等待接收消息时每 n=30 秒,因此它们可以提供信息关于经过的时间)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.