Suspend MessageQueue.BeginReceive Operation...

G

Guest

I have been unable to locate an answer for this question because (1) it is
late in the day and my eyes are tired of looking at code and documentation,
(2) I am new to this area of the .NET framework, or (3) a combination of (1)
and (2).

I am looking at using messaging in an application I am working on. I have
been reading the documentation about messaging and have been able to
understand just about everything that I have read so far. I don't see it as
being a requirement, but how do you suspend an asynchronous receive operation
on a message queue? Once the BeginReceive method is called, I would like to
stop the MessageQueue object from listening for new messages.

Thank you,

Jason Richmeier
 
L

Luke Zhang [MSFT]

The MessageQueue class is a wrapper around Message Queuing, you can use
Create(String), Delete, and Purge methods to manage queues on the server,
but you cannot block the MessageQueue to recieve new messages. Also, I
didn't think there is any reason to block new message when BeginReceive is
called. What is your opinion?


Luke Zhang
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
G

Guest

I am not so much interested in blocking new messages as I am suspending or
cancelling the receipt of new messages.

For example, say I have a windows service that monitors for new messages and
this service repeatedly calls the BeginReceive method to watch for new
messages. When the service is paused, I don't want to monitor for new
messages and when the service is resumed, I once again want to monitor for
new messages.

I have tried to figure out a way to cancel the asynchronous receive
operation when the service is paused but I haven't found anything yet. The
best I have come up with is to use a shared flag of some sort and update it
when the service is paused and resumed. Then, I would use a BeginPeek
instead of a BeginReceive (since the latter removes messages from the queue).
When a new message became available, I would check the value of the flag to
see if the service was currently running and receive the message if it was.
This seems like a rather odd way of doing it but it is the best I have been
able to come up with so far.

Any suggestions?

Thank you,

Jason Richmeier
 
L

Luke Zhang [MSFT]

When the service is paused, you may simply call the MessageQueue's Close
method to close it direclty. When the service is resume or restart, create
a new MessageQueue object to receive the message. Can this work for your
system?

Luke Zhang
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
G

Guest

I believe I understand your statement correctly so, unless I am doing
something wrong in my code, this method does not work.

In the OnStart method in my code, I create a MessageQueue object (with class
scope) and set its properties as necessary. Also in this method, I call the
BeginReceive method of this MessageQueue object.

In the OnPause method in my code, I call the Close method of the
MessageQueue object that was created in the OnStart method.

In the OnContinue method in my code, I reinitialize the MessageQueue object
(the same as what is done in the OnStart method).

For now, when a message is received in the message queue, I write an entry
to the event log. If the service is running and I send a message to the
queue, an entry is made in the event log. If I pause the service and send
another message to the queue, another entry is made in the event log.
However, the message is not removed from the queue. Once I resume the
service, another entry is made in the event log and the message is removed
from the queue. So, if I send one or more messages to the queue after
pausing the service, an extra entry is made in the event log.

I read a document that lead me to believe that in order to prohibit this
behavior, the property EnableConnectionCache should be set to false. I did
this but the described behavior continues.

Jason Richmeier
 
L

Luke Zhang [MSFT]

Hello Jason,

Thank you the information, However, I am a little confused by what you
said, how did you write an entry to the event log, in the windows service
application or external application? When the service is paused, did you
send messages to the queue, from external application?

Thanks,

Luke Zhang
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top