What's keeping you from using a thread?

Something like:

Thread mainThread;

protected override void OnStart(string[] args)
{
    string queuePath = @".\private$\myQueue";
    MessageQueue myQueue;
    if (! MessageQueue.Exists(queuePath))
    {
       MessageQueue.Create(queuePath);
    }
    myQueue = new MessageQueue(queuePath);

    mainThread = new Thread(new ThreadStart(threadMethod));
    mainThread.Start();
 }

private void threadMethod()
{
    // ...and now the infinite loop...
    while (true)
    {
       Message myMessage = myQueue.Receive();
       ProcessMessage(myMessage.Body.ToString()); // call to another method
    }
}

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