Service OnShutdown is not called

L

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);
}
 
G

Guest

Try commenting out the line where you write to the eventlog. Instead, just
write something to a file to see that the OnShutdown handler has fired.
Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top