Federico
Federico

Reputation: 799

Catching unhandled exception Windows Service

I've written a window service like this:

  public partial class Service1 : ServiceBase
{
    private Thread writerThread;
    private Thread messageThread;
    private bool stopNow;

    public Service1()
    {
    }

    private void MessageThread()
    {
        while (stopNow == false)
        {
            try
            {
               //Do Something
                Thread.Sleep(10000);
            }
            catch (Exception e)
            {
                GLogWriter.Write(String.Format("Critical exception {0}. Inner Exception {1} ", e.Message, e.InnerException.Message), LogCategories.Generic, TraceEventType.Critical);
                OnStop();
            }
        }
    }

    private void WriterThread()
    {
        bool checkGruppoIndirizzi = true;

        while (stopNow == false)
        {
            try
            {
                //Do something else
                Thread.Sleep(10000);
            }
            catch (Exception e)
            {
                GLogWriter.Write(String.Format("Critical exception {0}. Inner Exception {1} ", e.Message, e.InnerException.Message), LogCategories.Generic, TraceEventType.Critical);
                OnStop();
            }
        }
    }

    protected override void OnStart(string[] args)
    {
        GLogWriter.Write("SERVICE IS STARTING", LogCategories.Generic, TraceEventType.Information);

        this.stopNow = false;

        writerThread = new Thread(new ThreadStart(this.WriterThread));
        writerThread.Start();

        this.messageThread = new Thread(new ThreadStart(this.MessageThread));
        this.messageThread.Start();
    }

    protected override void OnStop()
    {
        GLogWriter.Write("SERVICE IS STOPPING", LogCategories.Generic, TraceEventType.Information);
        stopNow = true;
        writerThread.Join(1000);
        messageThread.Join(1000);
        GLogWriter.Write("SERVICE STOP SUCCESSFUL", LogCategories.Generic, TraceEventType.Information);
    }
}

}

The problem is when i Catch an exception apparently the OnStop() is not being called and the service stops promptly without logging the "SERVICE IS STOPPING" messages

Upvotes: 0

Views: 1692

Answers (3)

Pavel Zhuravlev
Pavel Zhuravlev

Reputation: 2791

OnStop() method is not something that should be called when the thread(s) of your service stop working. It is a special procedure which is supposed to be called by Service Control Manager. In other words, OnStop() is the procedure that should stop the service normally, when you need to. Something calls OnStop() -> service is stopped. It is not the other way around (something happens within the service -> thread(s) exit -> OnStop() is called)

Upvotes: 1

Abhinav
Abhinav

Reputation: 2085

This is because of the following line:

GLogWriter.Write(String.Format("Critical exception {0}. Inner Exception {1} ", e.Message, e.InnerException.Message), LogCategories.Generic, TraceEventType.Critical);

InnerException is not always an object, rather it is often null.

Change that line to:

GLogWriter.Write(String.Format("Critical exception {0}. Inner Exception {1} ", e.Message, ((e.InnerException == null) ? string.Empty : e.InnerException.Message)), LogCategories.Generic, TraceEventType.Critical);

Upvotes: 0

Rajesh Subramanian
Rajesh Subramanian

Reputation: 6490

Please look at Windows Event Log to see what kind of error or exception you are getting. If it is a program exception , the definitely it would have been caught on catch. So i suspect that service throw exception at system level.

That should be diagnosed through Event Log.

Upvotes: 1

Related Questions