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
>    }
> }
>  
> 

Reply via email to