473,662 Members | 2,546 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

When does the service's OnShutDown() gets called ?

308 Contributor
Hi ,

I want to commit the chages/ want to do some activity on service's OnShutuDown().B ut i found that It neven get's called ..when i shut down the system or when i power it off..in Both the cases call to OnShutDown not found.
I have my application running on Vista\Win server 2008 . CAn anybody tell me when does this OnShutDown() gets called.. ?

Thanks!
Sep 3 '08 #1
14 10411
balabaster
797 Recognized Expert Contributor
I want to commit the chages/ want to do some activity on service's OnShutuDown().B ut i found that It neven get's called ..when i shut down the system or when i power it off..
On the contrary - OnShutDown() (when spelled correctly) gets called when the services is requested to stop. If it's not being called then something is stopping your service from closing down in a timely manner. I couldn't begin to hazard a guess at what that is without seeing any code.

The only thing I can think of is when you shut the system down it's slightly different than when you tell the service to stop in the services applet, in that if the service doesn't stop within the specified time frame - I think the default is 1 minute...unless you have Exchange Server installed and it is changed by default to 10 minutes (600000 milliseconds). Once this timeout occurs the service processes are terminated in an uncontrolled fashion, frequently their shutdown process isn't complete successfully, and in some cases the shutdown process isn't even started before the process is killed.

Sadly, I'm not sure if there's a way around this... maybe extend the timeout period to give your service more time to close. It requires a mod to the registry, so careful...

Here's the registry key:

HKEY_LOCAL_MACH INE\SYSTEM\Curr entControlSet\C ontrol\WaitToKi llServiceTimeou t

Just increase the value (remember, it's in milliseconds, so setting it to 60 thinking you're setting it to a minute won't help the situation). Of course, don't be shocked when it takes Windows longer to go down for a reboot.
Sep 3 '08 #2
madankarmukta
308 Contributor
Thanks for the update ..

You mean to say for every service there is the timeperiod defind under WaitToKillServi ceTimeout value in the registry which stops the service after this much period of time .. Right ..?

Ohh.. Thanks..

But I am a bit crazy about .. As you guessed .. that there might be something which is resisting my service from stoping in timely manner..If this is the case , what if there is the service with WaitToKillServi ceTimeout as 600000 , after every 10 minutes the servic's OnStutdown() should get called..i.e. The Logs should get printed .. as i did in my test application ..If I have not mistaken you point..Please correct me if I am wrong.

If there is something which is stoping my service in the timely manner.. what could be it.. Can you please help me.. can you give me any suggestions ..

As per as the code is concern..
I created the simple windows service .. overrided OnStop(), OnShutDown(),On customeCommand( ) fun.. Rest are the common.. constructors.. etc.

Please let me know your feedback over this..

Again thank you..
Sep 4 '08 #3
r035198x
13,262 MVP
On the contrary - OnShutDown() (when spelled correctly) gets called when the services is requested to stop....
I may have misunderstood the response but OnshutDown gets executed when the operating system is shutdown rather than when the service is stopped.
Sep 4 '08 #4
madankarmukta
308 Contributor
I may have misunderstood the response but OnshutDown gets executed when the operating system is shutdown rather than when the service is stopped.

Thanks for the Reply ..
But it doesn't get called when the systemt gets shut down..

Thanks!
Sep 4 '08 #5
PRR
750 Recognized Expert Contributor
OnShutDown() : get called when windows operating system is shutting down..
OnStop(): when the service is stopped either manually or otherwise...


To check for this try writing to a text file... onstart onstop n on shutdown...
Incase it doesnt seem to be working then there may be some error in the code ...
For best results use threads...
Sep 4 '08 #6
madankarmukta
308 Contributor
Sory.. But it OnShutDown() never gets called when operating system get's shut down ..

Thanks!
Sep 4 '08 #7
PRR
750 Recognized Expert Contributor
Sory.. But it OnShutDown() never gets called when operating system get's shut down ..

Thanks!
Can u post some code ? wat have you written on ur onshutdown()? try this

[HTML]
public void OnShutDown()
{
Thread t2 = new Thread(new ThreadStart(thi s.Write));
t2.IsBackground = true;

t2.Start();
}

public void Write()
{
using (StreamWriter sw = File.CreateText (@"c:\DD" +".txt"))
{
sw.WriteLine("T his is my file.");

sw.Close();
}


}
[/HTML]
Sep 4 '08 #8
madankarmukta
308 Contributor
Can u post some code ? wat have you written on ur onshutdown()?

Yes sure.. here is the OnshutDown which i overrriden..

protected override void OnShutdown()
{
//I am creating this file only for the windows version greater than 5.x
StreamWriter sw = File.CreateText (FILE_SHUTDOWN) ;
sw.WriteLine("S ystem Shutdown");
sw.Close();
}

The problem coming is FILE_SHUTDOWN is not gettong created .. which i am interested in

Thanks!
Sep 4 '08 #9
madankarmukta
308 Contributor
Can u post some code ? wat have you written on ur onshutdown()? try this

[HTML]
public void OnShutDown()
{
Thread t2 = new Thread(new ThreadStart(thi s.Write));
t2.IsBackground = true;

t2.Start();
}

public void Write()
{
using (StreamWriter sw = File.CreateText (@"c:\DD" +".txt"))
{
sw.WriteLine("T his is my file.");

sw.Close();
}


}
[/HTML]

Hi,

I added your code into my service. Rebuild it.. Then I shut down the system..
Restart it.. But not found the fil;e DD.txt..Was I wrong anywhere while validating the workaround.

Thanks!
Sep 5 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

6
1638
by: Angus Comber | last post by:
Hello I have a base class a bit like this: class CTapiCall { // Constructor public: CTapiCall(); virtual ~CTapiCall();
27
2115
by: tuvok | last post by:
Is it correct that the virtual dtor of base gets called implicitly? Here's some code to demonstrate what I mean: Class B has a virtual destructor, so has class D which is derived from B. Deleting D calls the dtor of D and then the dtor of B. I was thinking that this would be true only for non-virtual dtor case, but I wouldn't have expected it happen for a virtual dtor. For a class with a virtual dtor I would have expected that only the...
3
3206
by: Vamsi | last post by:
Hello, I am working in VC++ environment.while using gdi+, i want to use a public member variable of Bitmap. which needs to be initialized at the time of creation. and i m using this gdi+ in a Dll application. i am trying to initialize the bitmap object at the constructor. like Bitmap m-bmpobj; // Bitmap Obj which is a private member of the TextEffect Class
7
2263
by: cgamache | last post by:
Which is a more correct statement? A destructor runs automatically when an object is deallocated. or A destructor runs automatically immediately before an object is deallocated.
4
1239
by: genc ymeri | last post by:
Hi I have placed an encounter code in the session_end as well in the session_start. I'm decreasing the number with 1 everytime a session ends. I open several IE-s and I closed some of them but the number of sessions only increase. When really the session_end code get called ? I thought that once we close the IE, the session go to go, and the session_end gets to get called ! Thaks in advance, Genc Ymeri.
2
1459
by: mswc.net | last post by:
I have put code inside this method (Global.asax) but not even after re-starting my machine or the aspnet_wp.exe service it seems to execute the code inside of it. When does this methods really gets called? Any ideas? Thanks.
0
2047
by: dotnw | last post by:
I have a server side checkbox with a javascript function attached to the onclick event. If I click on the checkbox's text itself (not the actual box), the javascript function gets called twice. Interestingly, if I upload this test project to the internet, and re-try this test, the javascript function correctly gets called only once. Also, if I re-try this test locally on my Win 2000 box, the function
8
3851
by: Allerdyce.John | last post by:
Hi, Can you please tell me when does the destructor of a static object gets called? Thank you.
11
2693
by: fiefie.niles | last post by:
I am using ASP.NET 2005 and I have a simple form. Page_Load calls a sub mySub that does not do anything (for testing purposes). But, Page_Load gets called twice. On every single ASPX page in my site, Page_Load gets called twice. Why is that and how can I fix this problem ? Thank you very much. Imports System.IO Namespace myProject Partial Class WebForm1
0
8432
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8344
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8857
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8546
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6186
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4180
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4347
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1993
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1752
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.