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?