Michi-2142
Michi-2142

Reputation: 1312

How does a windows service behaves on Windows shutdown?

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?

Upvotes: 4

Views: 7542

Answers (1)

Adriano Repetti
Adriano Repetti

Reputation: 67128

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.

Upvotes: 5

Related Questions