Is there a timeout for stopping Windows Services when Windows is shutting down? I know there is a registry key "WaitToKillServiceTimeout" but does this timeout effect on services, when Windows is shutting down or is there another timeout?
I want to find out, how long system during shutdown waits for services when it calls the OnStop method of a service.
Can a service prevent Windows from shutting down?
From TechNet:
[WaitToKillServiceTimeout] Determines how long the system waits for services to stop after notifying the service that the system is shutting down.
Then yes, that's value used to wait for services shutdown but also note that it's shared for all services:
If all services stop before this value expires, the system shuts down...
For your question "Can a service prevent Windows from shutting down?" answer is "more or less". You can't prevent Windows to shutdown (it may be really annoying for users if they can't shutdown because a service decided they shouldn't) but:
When the value of this entry expires, the system notifies the user that the service has not stopped. The user can either force the service task to stop or continue to wait. If the user waits, this value specifies the interval between repeated user notices that the service has not stopped.
Note that:
Some services increase the value of this entry to provide more time for cleanup tasks.
In case your service is slow to shutdown you may increase such value (during installation) to give you more time. In theory using a very high value you'll postpone shutdown for a very long time (but this behavior isn't same as cancel it and don't forget it's shared for all services).
AFAIK a service can't cancel shutdown but a GUI application can do it using ShutdownBlockReasonCreate() function.
Related
We have an OWIN application running on IIS and are currently using the AppProperties(IAppBuilder.Properties).OnAppDisposing cancellation token to detect when the application is being shutdown to gracefully shutdown some of the long running tasks that we start when the application starts.
The problem is that when the web.config is modified (even if just doing a "touch web.config") then the OnAppDisposing cancellation token is cancelled and our long running tasks shut themselves down.
But the application pool is not actually recycled in these cases and so our long running tasks do not get restarted until the app pool is actually recycled.
How is one supposed to handle web.config changes in these instances? So that we can still detect OnAppDisposing and shutdown gracefully while also not have to worry about long running tasks shutting down and not getting started again?
Update: I've found that the OWIN app will get initialized again when the first http request comes in. But if there were Hangfire or other Background tasks that you expected to be running they won't be started again until that web request comes in. Even if you have application initialization setup, the appinit url is not hit after the app domain restart.
Currently I am using Windows Service to keep on running some jobs in the background. These jobs are scheduled via Quartz.Net. Generally these are long running jobs, spans from 5 min to 15 min. If a job is currently running and if User will try to Shutdown the Windows Service then it throws Exception: Windows service did not respond in a timely fashion.
Actually OnShutdown() method of WindowsService, I am trying to Shutdown the Quartz.Net also.
I am calling scheduler.Shutdown(true) to stop the scheduler & wait for running jobs. If I will make this False then Windows Service Stops normally but it will put the running Job in Unstable state. Please help me on this.
You get this error because your service did not complete the Stop in the agreed-upon time. So, the SCM assumes it's hung in some way and aborts it.
To tell the SCM you need more time to complete the operation, call ServiceBase.RequestAdditionalTime() in your OnStop handler. For more information see http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicebase.requestadditionaltime.aspx
I have several important Quartz events that MUST go off at specific times of the night. Lately I have been noticing that not all the events are run. I have a feeling that overnight our server load is very light (ie. zero users) and that the web server kind of goes to sleep, and hence so does Quartz. Does this seem plausible? I am using Quartz.net within the web server, and not as a separate service.
Yes, that is plausible. In general, it is considered a bad practice to have IIS run scheduled tasks, that is a job better left for a Windows Service, or the built-in Windows Scheduled tasks (which has been much improved for Windows Server 2008).
Your worker process might have been shut down because there are no load. By default, IIS shuts down worker processes after 20 minutes without ingoing requests (you can alter this in the Application Pool settings). Also, worker processes are likely to be restarted after a certain amount of time or requests, or if they consume too much memory.
A quick-fix for your specific problem might be to use the Windows Task Scheduler to request the site periodically to keep it alive - or have it request a URL that triggers your task at the predefined time.
ASP.Net, by default, will shut down AppDomain's after a period of inactivity.
The recommended course of action is to implement timed events either in a Windows Service or as an executable launched from Window's Scheduled Tasks.
It is also possible to change the IIS configuration so that it will not shut down your AppDomain. How exactly this is accomplished varies between versions of IIS, but instructions can easily be found by searching.
IIS 7.5 introduces the notion of auto-start providers, that allow you to get WAS to auto-load an application or assemblies when an application pool starts up.
Can a similar thing be achieved with IIS7?
Basically, we have an application that runs under WAS, and has an in-memory cache of data. When an application pool recycle occurs, my WAS deployed app won't actually be activated until the first hit for it is received. This means that the cache is cold when the first hit is received. It would be good to be able to pre-start the application as soon as the app pool is recycled.
Other options we've considered are:
Deploying the application as a Windows service so it doesn't re-cycle (this would work, but the application lifecycle management of IIS/WAS is a useful thing apart from this issue)
Writing a separate service whose job is to ping our application to warm it up.
However, the nicest way would be to get IIS7/WAS to do this for us.
In Windows 2008 you can log events that occur on the application pool, so you can log recycle events.
You can configure the event viewer to start a program when a specified message has been logged. You could call your service or load the assemblies in that program.
Would this be a feasible solution for you?
Regards,
Michel
in the advanced settings of your application pool set your application pool to generate Recycle event log entry every time it is recycled; I think the option is "Specific Time". Then you can use Windows Task Scheduler, create a script or something you want it to run that will hit your site so it can initialize. Set the trigger for the task to an even, set the Event Filter that you want to trigger the task and voila.
I'm having an issue that I can't figure out regarding a Windows Service I have written in C#. What I am trying to do is wait for a specified period for a child process to terminate before I am forced to kill it and stop the service. I have set my timeout for anywhere from 5 to 15 seconds and my service waits the appropriate amount of time and then kills the child and stops when I stop it via the mmc service window. However if I shutdown the computer my service gets blown away before it is able to wait and stop properly. I have read that Windows allows for a timeout of 30 seconds for a service to shutdown before it blows it away. My code is executing in much less time than 30 seconds, max is around 20 seconds or so, depending on what I set the timeout at.
I have tried using the SetServiceStatus() function in the win32 Api but it has not changed the function of the code. I have verified that the function call is succeeding. Is there any other way to force Windows to wait for my service to shutdown properly? I am testing it in Windows 7 x86, and Windows Vista Sp1 x86.
Windows will not let any service prevent shutdown, this is by-design. as Gyuri says, you have to consider a different design for your app.
This is a global Windows setting, apparently: http://support.microsoft.com/kb/305788. I would be worried if I had to rely on the users to let my service shut down. I would save my data periodically instead.
OnSTart and OnSTop are inherited from a base class which does quite a bit for you. All your implementations of these methods should do is start or stop your client code, windows will do the rest.
I'll caveat this with the fact that I use C# and .Net's implementation of ServiceBase does this, I cannot talk authoritatively about C/C++ code or APIs.