Home All Groups Group Topic Archive Search About

Service OnShutdown is not called

Author
19 Apr 2007 3:38 PM
lb.weissman
Hi,

I have a simple service, which I want to stop normally. I added the
method OnShutdown, and set the CanShutdown property to true. Still,
when I restart the computer my OnShutdown method is not called.
The OnStop and OnStart methods are being called.
Any idea why?

Code:


        public ServiceController()
        {
            InitializeComponent();

            if (!
System.Diagnostics.EventLog.SourceExists("ServiceController"))

System.Diagnostics.EventLog.CreateEventSource("ServiceController",

String.Empty);

            eventLog1.Source = "ServiceController";
            if (eventLog1.OverflowAction !=
OverflowAction.OverwriteAsNeeded)
            {

eventLog1.ModifyOverflowPolicy(OverflowAction.OverwriteAsNeeded, 0);
            }
            // the event log source by which
            //the application is registered on the computer

            eventLog1.Log = String.Empty;
        }

        private void InitializeComponent()
        {
            this.eventLog1 = new System.Diagnostics.EventLog();
            ((System.ComponentModel.ISupportInitialize)
(this.eventLog1)).BeginInit();

            this.ServiceName = "ServiceController";
            this.CanShutdown = true;
            this.CanHandlePowerEvent = true;

            ((System.ComponentModel.ISupportInitialize)
(this.eventLog1)).EndInit();

        }

        protected override void OnStart(string[] args)
        {
            try
            {
                this.ServiceControllerServer = new
ServiceControllerServer(this.eventLog1, true);
                this.ServiceControllerServer.Run();

            }
            catch (Exception ex)
            {
                eventLog1.WriteEntry(ex.GetType().ToString() + "\n\n"
+ ex.Message + "\n\n" + ex.StackTrace);
            }
        }

        protected override void OnStop()
        {
            this.ServiceControllerServer.End(true);
        }

        protected override void OnShutdown()
        {
            eventLog1.WriteEntry("OnShutdown");
            this.ServiceControllerServer.End(true);
        }

AddThis Social Bookmark Button