473,403 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Service OnShutdown is not called

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

Apr 19 '07 #1
2 5718
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

--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"lb*********@gmail.com" wrote:
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);
}

Apr 19 '07 #2
That worked!
thanks.

Apr 19 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: tascien | last post by:
I created a window service... When i stop the service through MMC, it can see in the EventLog that it calls OnStop() within the service... and all stop handling routines are processed...
0
by: bj7lewis | last post by:
I have C# service(VS.NET 2002) that I want to bk up a few files when Windows is shutdown/restart(thur any of the correct ways to Shutdown/restart window) and choose to do the actual backup in a...
0
by: TDC | last post by:
At system start I get the OnStart. When the system shuts down, do I *just* get an OnShutdown, or will I get both and OnStop and an OnShutdown? If you only get the OnShutdown, I find it odd that...
2
by: Peter Meinl | last post by:
Restarting Windows seems not to fire the OnShutdown event in the Windows Service. Does not work on Windows XP SP2 and not on Windows 2003 Server SP1. OnStart and OnStop do work fine. Steps...
1
by: chitra g | last post by:
Hi, I tried all the options below but did not work. Your suggestions please.
2
by: Bill Davidson | last post by:
All: I have a Win32 service that takes about 30 seconds to shutdown (give or take a few seconds). I shut the service down via the 'Services' console on Windows Server 2003. When the service...
0
by: Klaus Unterberg | last post by:
Hello, does anyone know, how to make an EventLog in service event OnShutdown? It seems that OnShutdown cannot make an entry to the eventlog.writeentry(). Even when Eventlog is listed in the...
14
by: madankarmukta | last post by:
Hi , I want to commit the chages/ want to do some activity on service's OnShutuDown().But i found that It neven get's called ..when i shut down the system or when i power it off..in Both the cases...
16
by: ayush patel | last post by:
Hi, I have tried several ways to make this work but only found work arounds. so I am posting this as a last trial. I have a windows service which has to kick start and generate a file and email...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.