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("ServiceC ontroller"))
System.Diagnostics.EventLog.CreateEventSource("Ser viceController",
String.Empty);
eventLog1.Source = "ServiceController";
if (eventLog1.OverflowAction !=
OverflowAction.OverwriteAsNeeded)
{
eventLog1.ModifyOverflowPolicy(OverflowAction.Over writeAsNeeded, 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);
}