The Receive method of MessageQueue class is a thread-blocking method. The parameterless overload has an infinite timeout.
Exerpt from MSDN library: "Use a call to Receive when it is acceptable for the current thread to be blocked while it waits for a message to arrive in the queue. Because this overload of the Receive method specifies an infinite time-out, the application might wait indefinitely. If the application processing should continue without waiting for the message, consider using the asynchronous method, BeginReceive." -----Mensaje original----- De: Duncan Godwin [mailto:[EMAIL PROTECTED] Enviado el: mi�rcoles, 04 de junio de 2003 22:15 Para: [EMAIL PROTECTED] Asunto: Re: [ADVANCED-DOTNET] Message Queue and Windows Service Hi, Have a look at the System.Timers namespace, set an interval and your code will callback on that interval which is far more efficient in my opinion than continually looping. This namespace has been designed with windows services in mind, I usually put the poll interval in the application configuration. e.g. private MessageQueue myQueue; private void InitQueue() { string queuePath = @".\private$\myQueue"; if (! MessageQueue.Exists(queuePath)) { MessageQueue.Create(queuePath); } myQueue = new MessageQueue(queuePath); } protected override void OnStart(string[] args) { InitQueue(); System.Timers.Timer aTimer = new System.Timers.Timer(); aTimer.Elapsed+=new ElapsedEventHandler(this.OnMSMQCheck); // Set the Interval to 5 seconds. aTimer.Interval=5000; aTimer.Enabled=true; } // Specify what you want to happen when the Elapsed event is raised. public void OnMSMQCheck(object source, ElapsedEventArgs e) { { // a while more messages or something? Message myMessage = myQueue.Receive(); ProcessMessage(myMessage.Body.ToString()); // call to another method } ok it's rough and I haven't tested compiling it, but it or something close should work. Kind Regards, Duncan ----- Original Message ----- From: "Zecharya Bar" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Sent: Wednesday, June 04, 2003 2:28 PM Subject: [ADVANCED-DOTNET] Message Queue and Windows Service > Hi, > > I'm beginning to experiment with Microsoft Message Queues and have run into a simple > problem. > I would like a Windows Service I'm creating to monitor continuously a MSMQ, and upon > receiving a message, call another method. > How do I get the service to continually check the queue? > I've tried putting the Receive() function in an infinite loop in the OnStart method > (see code), but of course that keeps the service from installing! > > I'd appreciate any advice! > > Bar > > *** my code *** > protected override void OnStart(string[] args) > { > string queuePath = @".\private$\myQueue"; > MessageQueue myQueue; > if (! MessageQueue.Exists(queuePath)) > { > MessageQueue.Create(queuePath); > } > myQueue = new MessageQueue(queuePath); > > // ...and now the infinite loop... > while (true) > { > Message myMessage = myQueue.Receive(); > ProcessMessage(myMessage.Body.ToString()); // call to another method > } > } > >
