Mar 29, 2014 07:59 AM|AidyF|LINK
The thing about a Windows Service is that it is always running so you have to know how to deal with the occassions when nothing is happening as you can't write code like
} while (true);
as it hammers resources. The advantage of using msmq is that you can get your code to just wait until something comes on the queue. So your code is sleeping and "wakes up" when something hits the queue. If you wanted to poll a database then you'd need
to handle your own sleep management. The most basic way of doing this is
That will check if anything needs done and then just wait for 5000s so your service only "wakes up" briefly once every 5 seconds. Writing services it outside the scope of this forum but there are better ways than Sleep such as the AutoResetEvent. When
you're writing a service that sleeps you have to remember that your code needs to be able to react to a Stop or Restart command from the service manager. So most services kick of a work thread on a new thread so that the original thread can respond to a Stop.
Then the work thread ususes AutoResetEvent so the original thread can wake it up when a Stop command comes in. If you google for writing windows services you'll find this kind of code fiarly easily.