0

I have made a service that reads messages from a MSMQ and then converts these before pushing them to a service bus.

It is possible that the processor fails on a message, which I catch and send the message to another MSMQ queue, and when a certain amount of these failures occur, I stop the server.

How I do this is by calling the OnStop() Method.

This works, and the server shuts down, however, when I check the Services screen in windows, the server is still there, shown as running.

How can I make it so that the Services screen knows that my service has stopped as well?

Relevant bits of code:

Handler that is called for each message retrieved from the MSMQ

private void Mq_ReceiveCompleted(object sender, ReceiveCompletedEventArgs e)
    {
        FailureCheck();

        if (!failure)
        {
            // Process message
        }
        else
        {
            OnStop();
        }
    }

OnStop () Method

protected override void OnStop()
    {
        try
        {
            MessageQueue.ReceiveCompleted -= Mq_ReceiveCompleted;
            System.Threading.Thread.Sleep(2 * 60 * 1000);
            MessageQueue.Dispose();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

Everything works, the service is being stopped, it waits the full two minutes and then shuts down, but the Services screen shows it as still running which forces me to click shut down manually there. Is there a way to let it know that the service is being shut down and update its UI?

Agnel Amodia
  • 765
  • 8
  • 18
Robin
  • 2,704
  • 7
  • 30
  • 47
  • 1
    You need to inform the SCM as it will not happen automatically, see [What is the proper way for a Windows service to fail?](https://stackoverflow.com/questions/4197036/what-is-the-proper-way-for-a-windows-service-to-fail) – Alex K. Mar 19 '18 at 14:58
  • @AlexK. When I use the Stop() method the service does not seem to come in the OnStop() method, is that correct? – Robin Mar 19 '18 at 15:02
  • The service manager should have showed you an error message, services must respond to a command within 30 seconds. Why you did not see it is hard to guess. So in all likelihood it did actually stop, the service manager just didn't see it because it gave up waiting. – Hans Passant Mar 19 '18 at 15:06

1 Answers1

2

Please call the Method Stop() - with OnStop() you are executing the method which is invoked when the service is stopping but if I remember correctly does not stop the service itself. MSDN

Further note, that no thread or anything else may be still running. This can prevent your service from stopping correctly.

dsdel
  • 1,042
  • 1
  • 8
  • 11
  • When I call the stop method, the service does not enter the OnStop method, is this correct? I assumed that the OnStop method would be called from the Stop method. – Robin Mar 19 '18 at 15:03
  • 1
    @Robin, OnStop should be executed, see [MSDN OnStop](https://msdn.microsoft.com/en-us/library/system.serviceprocess.servicebase.onstop(v=vs.110).aspx). Is CanStop set to true? – dsdel Mar 19 '18 at 15:19
  • I have it working now, I think there was a small issue with the timers I used to wait for threads finishing their business. It works correctly now it seems. Thanks! – Robin Mar 19 '18 at 15:25