任何人都可以确定为什么一次收听MSMQ,然后退出?我认为DoWhile()
会让它保持监听,但是当启动时队列中有消息时,它会处理它们一次然后退出。在StopRequested
之前是否有更合适的倾听方式?
public virtual void RunProcessor()
{
_stopRequested = false;
MessageQueue mq = null;
try
{
//create a subscribable stream from the msmq async BeginRecieve event
mq = GetMessageQueue();
var msmqListener =
Observable.FromAsync(() => Task<Message>.Factory.FromAsync(mq.BeginReceive(), mq.EndReceive));
Subscribe(msmqListener.DoWhile(() => !StopRequested));
}
catch (Exception ex)
{
if ((ex is ThreadAbortException) || (ex is ThreadInterruptedException)) throw;
_logger.Error("Alert processor thread threw an exception", ex);
}
finally
{
mq?.Close();
}
}