473,508 Members | 2,281 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().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 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 10399
balabaster
797 Recognized Expert Contributor
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..
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_MACHINE\SYSTEM\CurrentControlSet\Contro l\WaitToKillServiceTimeout

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 WaitToKillServiceTimeout 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 WaitToKillServiceTimeout 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(),OncustomeCommand() 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(this.Write));
t2.IsBackground = true;

t2.Start();
}

public void Write()
{
using (StreamWriter sw = File.CreateText(@"c:\DD" +".txt"))
{
sw.WriteLine("This 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("System 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(this.Write));
t2.IsBackground = true;

t2.Start();
}

public void Write()
{
using (StreamWriter sw = File.CreateText(@"c:\DD" +".txt"))
{
sw.WriteLine("This 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
PRR
750 Recognized Expert Contributor
What account your service is running? in my service .. on shut down i had written code which did something like serializing n de-serializing class n writing log file... it worked alright... try n run the service under an account that can write files to disk...or to event logs..
Sep 5 '08 #11
madankarmukta
308 Contributor
What account your service is running? in my service .. on shut down i had written code which did something like serializing n de-serializing class n writing log file... it worked alright... try n run the service under an account that can write files to disk...or to event logs..

ohh.. Thanks!

Soryy for saying.. But still it's not working.. :(

Thanks!
Sep 5 '08 #12
balabaster
797 Recognized Expert Contributor
ohh.. Thanks!

Soryy for saying.. But still it's not working.. :(

Thanks!
Hmm, untested thoughts: I'm wondering if one of a couple of things are occurring:
1). The system shutdown is preventing new threads from starting, and consequently it's preventing your thread which writes to the file from starting.
2). The completion of the shutdown of your service is occurring before the write thread completes and thus causes the thread to abort in an uncontrolled fashion without completing it's task - somewhat similar to forcing an application to exit before all threads are complete. The application doesn't wait for the threads to complete, it just aborts them all.

You could test either of these theories by calling the write method directly in your OnShutdown() a la:

Expand|Select|Wrap|Line Numbers
  1. protected override void OnShutdown()
  2. {
  3.   streamwriter sw = new file.CreateText("c:\myfile.txt");
  4.   sw.WriteLine("OnShutdown method fired...");
  5.   sw.close;
  6. }
It's a shot in the dark, I really have no clue what is causing the problem, as it appears to me that your code is logically sound.
Sep 5 '08 #13
madankarmukta
308 Contributor
Hmm, untested thoughts: I'm wondering if one of a couple of things are occurring:
1). The system shutdown is preventing new threads from starting, and consequently it's preventing your thread which writes to the file from starting.
2). The completion of the shutdown of your service is occurring before the write thread completes and thus causes the thread to abort in an uncontrolled fashion without completing it's task - somewhat similar to forcing an application to exit before all threads are complete. The application doesn't wait for the threads to complete, it just aborts them all.

You could test either of these theories by calling the write method directly in your OnShutdown() a la:

Expand|Select|Wrap|Line Numbers
  1. protected override void OnShutdown()
  2. {
  3.   streamwriter sw = new file.CreateText("c:\myfile.txt");
  4.   sw.WriteLine("OnShutdown method fired...");
  5.   sw.close;
  6. }
It's a shot in the dark, I really have no clue what is causing the problem, as it appears to me that your code is logically sound.
Ohh Thanks..

I will try to test as you suggested..

I have some doubts to ask you.. about your very first answer to my post..
Here is that:


You mean to say for every service there is the timeperiod defind under WaitToKillServiceTimeout 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 WaitToKillServiceTimeout 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(),OncustomeCommand() fun.. Rest are the common.. constructors.. etc.


About OS : My service running on Windows serverv 2008 / Vista

Please let me know your feedback over this..

Thanks
Sep 5 '08 #14
balabaster
797 Recognized Expert Contributor
Ohh Thanks..

I will try to test as you suggested..

I have some doubts to ask you.. about your very first answer to my post..
Here is that:


You mean to say for every service there is the timeperiod defind under WaitToKillServiceTimeout 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 WaitToKillServiceTimeout 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(),OncustomeCommand() fun.. Rest are the common.. constructors.. etc.


About OS : My service running on Windows serverv 2008 / Vista

Please let me know your feedback over this..

Thanks
The WaitToKillServiceTimeout is only (as far as I'm aware) evaluated as the system is shutting down - I don't believe it is constantly revisited throughout the lifetime of the Windows Session, just the one time as you're closing down.

When you ask the system to restart/shutdown, it issues the shutdown command to each of the active services and then waits either for the period specified in the registry key (oddly, it doesn't appear to shutdown earlier if all the processes are shut down successfully prior to the completion of that time period), so for instance, if you have Exchange installed, and the WaitToKillServiceTimeout has been increased to 10 minutes, but on shutdown all the services still complete their shutdown processes in let's say 2 minutes and 30 seconds...the system still sits there and dutifully waits the full 10 minutes - whoever's idea that was, was a genius (note the sarcasm in my voice). Once the period has elapsed, the system indiscriminately kills all remaining active services thus removing them from memory in whatever state they were in, at the time they were killed.

Now, if something is preventing your service shutting down in a timely manner, I'm not sure and not really able to tell. The only way to really figure this out is to have the very first statements in your OnShutdown() method to spit out some form of output to say that the shutdown method has at least been called. Whether or not it shuts down in a timely manner is due to whatever other code is inside your method. If it never reaches the end of the method, then it is deemed to have not shut down in a timely manner and will be killed off when the WaitToKillServicesTimeout period has elapsed... but at the very least, some of the calls inside the method should have been executed.
Sep 5 '08 #15

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

Similar topics

6
1631
by: Angus Comber | last post by:
Hello I have a base class a bit like this: class CTapiCall { // Constructor public: CTapiCall(); virtual ~CTapiCall();
27
2099
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...
3
3198
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...
7
2250
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
1225
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...
2
1448
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...
0
2037
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....
8
3846
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
2680
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...
0
7225
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
7324
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,...
0
7382
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...
1
5052
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...
0
4707
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3193
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...
0
3181
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1556
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
766
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.