Use BeginRecieve(). With BeginRecieve, you won't block in a hard loop.
When a message is recieved in the queue, an event is fired that there is a
message in the queue. here is the info on BeginRecieve:
ms-help://MS.NETFrameworkSDKv1.1/cpref/html/frlrfSystemMessagingMessageQueue
ClassBeginReceiveTopic1.htm
Wally
----- Original Message -----
From: "Zecharya Bar" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Wednesday, June 04, 2003 9:28 AM
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
> }
> }
>
>