我正在使用 PeekCompleted 从 MessageQueue 读取消息,我在这里执行我的过程,如果一切顺利,我需要将其从队列中删除!目前我正在使用 MyMessageQueue.Receive() 并且它可以工作,但这是确保每条消息都得到正确处理的可靠方法吗?
MessageQueue MyMessageQueue;
public Form1()
{
InitializeComponent();
MyMessageQueue = new MessageQueue(@".\private$\Dms");
MyMessageQueue.PeekCompleted += new PeekCompletedEventHandler(MessageQueue_PeekCompleted);
MyMessageQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
MyMessageQueue.BeginPeek();
}
void MessageQueue_PeekCompleted(object sender, PeekCompletedEventArgs e)
{
try
{
Debug.WriteLine("ToProcess:" + e.Message.Body);
//Long process that maybe fail
MyMessageQueue.Receive();
}
finally
{
MyMessageQueue.BeginPeek();
}
}