title: Multi threaded Windows service to process Windows Message Queue
body: <p>This is my first attempt at writing a Windows service. </p>
  
  <p>This windows service has to process 2 windows message Queues.</p>
  
  <p>Each Message Queue should have there own thread, but I can't seem to get the Architecture in Place.</p>
  
  <p>I followed this <a href="https://stackoverflow.com/questions/4864673/windows-service-to-run-constantly/4865893#4865893">Windows Service to run constantly</a> which allowed me to create one thread in which I am Processing one Queue.</p>
  
  <p>So this is my service class:</p>
  
  <pre><code>    protected override void OnStart(string[] args)
      {
          _thread = new Thread(WorkerThreadFunc) { Name = "Address Calculator Thread", IsBackground = true };
          _thread.Start();
      }
  
      private void WorkerThreadFunc()
      {
          _addressCalculator = new GACAddressCalculator();
  
          while (!_shutdownEvent.WaitOne(0))
          {
              _addressCalculator.StartAddressCalculation();
          }
      }
  
  
  
      protected override void OnStop()
      {
          _shutdownEvent.Set();
          if (!_thread.Join(5000))
          { // give the thread 5 seconds to stop
              _thread.Abort();
          }
      }
  </code></pre>
  
  <p>In My <code>GACAddressCalculator.StartAddressCalculation()</code> I am creating a Queue Processor Object which looks like this:</p>
  
  <pre><code>    public void StartAddressCalculation()
      {
          try
          {
              var googleQueue = new GISGoogleQueue("VehMonLogGISGoogle", 1, _gacLogger, 1);
              googleQueue.ProccessMessageQueue();
  
          }
          catch (Exception ex)
          {
  
          }
  
      }
  </code></pre>
  
  <p>And this is <code>GISGoogleQueue</code>:</p>
  
  <pre><code>public class GISGoogleQueue : BaseMessageQueue
  {
  
  
      public GISGoogleQueue(string queueName, int threadCount, GACLogger logger, int messagesPerThread)
          : base(queueName, threadCount, logger, messagesPerThread)
      {
      }
  
      public override void ProccessMessageQueue()
      {
          if (!MessageQueue.Exists(base.QueueName))
          {
              _logger.LogMessage(MessageType.Information, string.Format("Queue '{0}' doesn't exist", this.QueueName));
              return;
          }
  
          var messageQueue = new MessageQueue(QueueName);
          var myVehMonLog = new VehMonLog();
          var o = new Object();
          var arrTypes = new Type[2];
          arrTypes[0] = myVehMonLog.GetType();
          arrTypes[1] = o.GetType();
          messageQueue.Formatter = new XmlMessageFormatter(arrTypes);
  
          using (var pool = new Pool(ThreadCount))
          {
  
              // Infinite loop to process all messages in Queue
              for (; ; )
              {
                  for (var i = 0; i &lt; MessagesPerThread; i++)
                  {
                      try
                      {
                          while (pool.TaskCount() &gt;= MessagesPerThread) ; // Stop execution until Tasks in pool have been executed
  
  
                          var message = messageQueue.Receive(new TimeSpan(0, 0, 5, 0)); // TimeOut for message reading from Queue, set to 5 minutes, Will throw exception after 5 mins
  
                          if (message != null) // Check if message is not Null
                          {
                              var monLog = (VehMonLog)message.Body;
                              pool.QueueTask(() =&gt; ProcessMessageFromQueue(monLog)); // Add to Tasks list in Pool
                          }
  
                      }
                      catch (Exception ex)
                      {
  
                      }
  
                  }
              }
          }
      }
  </code></pre>
  
  <p>}</p>
  
  <p>Now this works fine for 1 Message queue, but if I want to process another message Queue it won't happen as I have an infinite loop in the <code>ProccessMessageQueue</code> method.</p>
  
  <p>I want to execute each Queue in a separate thread.</p>
  
  <p>I think I am making a mistake in <code>WorkerThreadFunc()</code>, I have to somehow start two threads from there or in the <code>OnStart()</code>.</p>
  
  <p>Also if you have any tips on how to improve this service would be great. </p>
  
  <p>By the way I am using the Pool Class from this Answer <a href="https://stackoverflow.com/a/436552/1910735">https://stackoverflow.com/a/436552/1910735</a> for the thread Pool inside <code>ProccessMessageQueue</code> </p>