1
votes

How can I stop a synchronous or asynchronous Receive from a MessageQueue in C#?

I find that if I haven't received any messages from the queue since I opened it, I can simply Close / Dispose the queue, and the Receive or EndReceive method will except, which is fine. However, if I've already successfully received a message from the queue, I can close and dispose the queue, but the pending or asynchronous receive will carry on regardless!

I've read elsewhere and found some suggestion that I should get EnableConnectionCache to false to ensure the message queue is closed when I ask, but it still seems to be ignore me regardless.

1
possible duplicate of .NET asynchronous MSMQ - Hans Passant
MS came up with the Begin/End pattern without thinking hard enough about cancellation :( I don't like that fact. Note even under the best circumstances, with cancellation there is usually a possible a race condition with completion -- it might be too far along that you can't cancel it in time before the callback is invoked. - Jason Kresowaty

1 Answers

0
votes

As binarycoder suggested, I have found that this is just a consequence of non-transactional queues. At least as far as MSMQ goes. A peek > receive pattern will solve the problem, but only if there is one reader. Otherwise, if you have the same issue you'll have to use transactional queues.