By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,499 Members | 1,303 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,499 IT Pros & Developers. It's quick & easy.

Service Stop timeout setting

P: n/a
Hello,

My c# based windows service takes a while to dispose. I have to release
bunch of resources all over the place and unfortunately it can take
20-40 seconds before I can cleanly exit.

Every now and then, when I stop the service from the Services applet, it
will timeout and bring up an error message saying that the service isn't
responding.

My questions:

1. What is the timeout value for stopping the service. It almost
seems variable and random, but maybe it's my imagination.
2. Is there a way to change the timeout from inside the service (as needed)?
3. Is there a way to communicate with the services applet and tell it
cancel the stop request?
4. Is there a way to communicate with the services applet and tell it
that the service is in the middle of something now and cannot be stopped
(a user-friendly message).

Thanks.
Nov 17 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
Frank,

You can declare the SetServiceStatus method to be called through
P/Invoke. While disposing your objects, you should make a call to
SetServiceStatus, passing a value of SERVICE_STOP_PENDING for the
dwCurrentState field of the SERVICE_STATUS structure that is passed to to
the function.

The ServiceHandle property will return the handle that you need to pass
as the first parameter to that function.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Frank Rizzo" <no**@none.com> wrote in message
news:OU*************@TK2MSFTNGP15.phx.gbl...
Hello,

My c# based windows service takes a while to dispose. I have to release
bunch of resources all over the place and unfortunately it can take 20-40
seconds before I can cleanly exit.

Every now and then, when I stop the service from the Services applet, it
will timeout and bring up an error message saying that the service isn't
responding.

My questions:

1. What is the timeout value for stopping the service. It almost seems
variable and random, but maybe it's my imagination.
2. Is there a way to change the timeout from inside the service (as
needed)?
3. Is there a way to communicate with the services applet and tell it
cancel the stop request?
4. Is there a way to communicate with the services applet and tell it that
the service is in the middle of something now and cannot be stopped (a
user-friendly message).

Thanks.

Nov 17 '05 #2

P: n/a
I fear that I do not have definite answers for your #3, however I do have
some for 1, 2 and 4.

1. Windows has a timeout of 30 seconds for services when they startup or
shutdown so if the desired operation has not completed in that window, it
kills the process.

2. AFAIK there is no way to dynamically change the timeout time which you
can specify as a REG_DWORD (in milliseconds) named ServicesPipeTimeout
inside of HKEY_LOCAL_MACHINE\System\CurrentControlSet\Contro l as this value
specirfied at the time of windows starting up will be what is used until
changed and rebooted.

4. If your service controlling app and your service are .NET applications,
look into the CanStop property of the ServiceBase and ServiceController
classes (or your derivatives of both). As you might guess from the name, you
can use it to get and set the ability of the service to be stopped after it
is started.

Brendan
"Frank Rizzo" wrote:
Hello,

My c# based windows service takes a while to dispose. I have to release
bunch of resources all over the place and unfortunately it can take
20-40 seconds before I can cleanly exit.

Every now and then, when I stop the service from the Services applet, it
will timeout and bring up an error message saying that the service isn't
responding.

My questions:

1. What is the timeout value for stopping the service. It almost
seems variable and random, but maybe it's my imagination.
2. Is there a way to change the timeout from inside the service (as needed)?
3. Is there a way to communicate with the services applet and tell it
cancel the stop request?
4. Is there a way to communicate with the services applet and tell it
that the service is in the middle of something now and cannot be stopped
(a user-friendly message).

Thanks.

Nov 17 '05 #3

P: n/a
I can help with generalities as I have written many services in all versions
of NT since 3.0 - but I haven't had to do services in awhile so my specifics
have faded...

Also, as convenient as the .NET service wrappers are they do not expose all
of the functionality that the WIN32 API implements. In order to do some of
the more advanced things you want to do you will have to do some PInvoke via
thes service APIs:

http://msdn.microsoft.com/library/de..._functions.asp
1. What is the timeout value for stopping the service. It almost
seems variable and random, but maybe it's my imagination.
I believe it is in the registry and you can change it. However it is a
machine wide setting so you need admin powers to alter it. Also, if you
shorten it you can easily cause somebody else's service to crash on exit...
{aka changing the default is not recommended}
2. Is there a way to change the timeout from inside the service (as needed)?
If the stop request has arrived already you've got to shut down in whatever
timeout window the system is configured for and as before, you probably don't
want to mess with the timeout setting...

HOWEVER you can make 'unsolicited' calls to SetStatus() while transitting
from running to stopped. Each time that you notify the SCM via
SetStatus("Stop Pending") the timeout counter is reset :). More info in the
docs for SetStatus()...
3. Is there a way to communicate with the services applet and tell it
cancel the stop request?
See also HandlerEx() - you can have a callback routine in your service that
responds to a custom service commands. Also you can {and I often do} have a
dedicated thread in my service that uses remoting or sockets, etc. to
communicate with the outside world.
4. Is there a way to communicate with the services applet and tell it
that the service is in the middle of something now and cannot be stopped
(a user-friendly message).


See .NET ServiceController class. You can set "CanStop = false" property
and the stop command is disabled for the service...

Also, not such a great idea but possible, you can attach to any/all user's
desktop's and display message boxes etc. Aside from being an infuriating
programming chore {trust me} this gets into issues like - to which desktop
shall I attach if there are multiple desktop's open on the machine, and is it
safe for me to throw up a, probably modal, dialog on a targeted desktop?? -
Yukk!!!

--Richard

Nov 17 '05 #4

P: n/a

"Frank Rizzo" <no**@none.com> wrote in message
news:OU*************@TK2MSFTNGP15.phx.gbl...
Hello,

My c# based windows service takes a while to dispose. I have to release
bunch of resources all over the place and unfortunately it can take 20-40
seconds before I can cleanly exit.

Every now and then, when I stop the service from the Services applet, it
will timeout and bring up an error message saying that the service isn't
responding.

My questions:

1. What is the timeout value for stopping the service. It almost seems
variable and random, but maybe it's my imagination.
2. Is there a way to change the timeout from inside the service (as
needed)?
3. Is there a way to communicate with the services applet and tell it
cancel the stop request?
4. Is there a way to communicate with the services applet and tell it that
the service is in the middle of something now and cannot be stopped (a
user-friendly message).

Thanks.


See Brendan's answer for 1,2 and 4.

3. No, there no way to cancel the request, but you can refuse to honor the
request. To do this simply throw an exception in your OnClose handler. Of
course this wont prevent a message to be displayed by the SCM and an error
message loged in the eventlog.
Note also that your service isn't communicating with the applet, it's
communicating with the SCM (services.exe). The control applet is a so called
Service Control Program which 'talks' to the SCM, there is no way for the
service to communicate back with the SCP.

Willy.

Nov 17 '05 #5

P: n/a

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:O8**************@TK2MSFTNGP10.phx.gbl...
Frank,

You can declare the SetServiceStatus method to be called through
P/Invoke. While disposing your objects, you should make a call to
SetServiceStatus, passing a value of SERVICE_STOP_PENDING for the
dwCurrentState field of the SERVICE_STATUS structure that is passed to to
the function.

The ServiceHandle property will return the handle that you need to pass
as the first parameter to that function.

Hope this helps.


No, this isn't possible you can't get the statusHandle required to call
SetServiceStatus, this handle is private to the ServiceBase class. v2.0 of
the framework exposes the SetServiceStatus API , this solves a number of
issues we have now in v1.x .

Willy.
Nov 17 '05 #6

P: n/a

"Richard" <Ri*****@discussions.microsoft.com> wrote in message
news:7B**********************************@microsof t.com...
I can help with generalities as I have written many services in all
versions
of NT since 3.0 - but I haven't had to do services in awhile so my
specifics
have faded...

Also, as convenient as the .NET service wrappers are they do not expose
all
of the functionality that the WIN32 API implements. In order to do some
of
the more advanced things you want to do you will have to do some PInvoke
via
thes service APIs:

http://msdn.microsoft.com/library/de..._functions.asp
1. What is the timeout value for stopping the service. It almost
seems variable and random, but maybe it's my imagination.


I believe it is in the registry and you can change it. However it is a
machine wide setting so you need admin powers to alter it. Also, if you
shorten it you can easily cause somebody else's service to crash on
exit...
{aka changing the default is not recommended}
2. Is there a way to change the timeout from inside the service (as
needed)?


If the stop request has arrived already you've got to shut down in
whatever
timeout window the system is configured for and as before, you probably
don't
want to mess with the timeout setting...

HOWEVER you can make 'unsolicited' calls to SetStatus() while transitting
from running to stopped. Each time that you notify the SCM via
SetStatus("Stop Pending") the timeout counter is reset :). More info in
the
docs for SetStatus()...

Unfortunately this doesn't work in v1.x, because you need a handle that is
returned from the RegisterServiceCtrlHandlerEx() API call, this handle is
kept private to the ServiceBase.
Willy.
Nov 17 '05 #7

P: n/a

"Brendan Grant" <gr****@NOSPAMdahat.com> wrote in message
news:BB**********************************@microsof t.com...
I fear that I do not have definite answers for your #3, however I do have
some for 1, 2 and 4.

1. Windows has a timeout of 30 seconds for services when they startup or
shutdown so if the desired operation has not completed in that window, it
kills the process.

2. AFAIK there is no way to dynamically change the timeout time which you
can specify as a REG_DWORD (in milliseconds) named ServicesPipeTimeout
inside of HKEY_LOCAL_MACHINE\System\CurrentControlSet\Contro l as this
value
specirfied at the time of windows starting up will be what is used until
changed and rebooted.

IMO. Should be WaitToKillServiceTimeout instead of ServicesPipeTimeout.
Willy.
Nov 17 '05 #8

P: n/a
You are correct, and from the looks of it I was partially mistaken. From the
looks of it, the two are used for separate things.

WaitToKillServiceTimeout is used only for shutting down a service, while
ServicesPipeTimeout on the other hand (to quote Microsoft Windows Internals,
Fourth Edition): “Determines the length of time that the SCM waits for a
service to call StartServiceCtrlDispatcher and connect before it gives up,
terminates the process, and concludes that the service failed to start.”

Also worth noting is that while ServicesPipeTimeout does have a default
timeout time of 30 seconds, WaitToKillServiceTimeout has a 20 second time
out...

Brendan
"Willy Denoyette [MVP]" wrote:

"Brendan Grant" <gr****@NOSPAMdahat.com> wrote in message
news:BB**********************************@microsof t.com...
I fear that I do not have definite answers for your #3, however I do have
some for 1, 2 and 4.

1. Windows has a timeout of 30 seconds for services when they startup or
shutdown so if the desired operation has not completed in that window, it
kills the process.

2. AFAIK there is no way to dynamically change the timeout time which you
can specify as a REG_DWORD (in milliseconds) named ServicesPipeTimeout
inside of HKEY_LOCAL_MACHINE\System\CurrentControlSet\Contro l as this
value
specirfied at the time of windows starting up will be what is used until
changed and rebooted.

IMO. Should be WaitToKillServiceTimeout instead of ServicesPipeTimeout.
Willy.

Nov 17 '05 #9

P: n/a

"Brendan Grant" <gr****@NOSPAMdahat.com> wrote in message
news:B3**********************************@microsof t.com...
You are correct, and from the looks of it I was partially mistaken. From
the
looks of it, the two are used for separate things.

WaitToKillServiceTimeout is used only for shutting down a service, while
ServicesPipeTimeout on the other hand (to quote Microsoft Windows
Internals,
Fourth Edition): “Determines the length of time that the SCM waits for a
service to call StartServiceCtrlDispatcher and connect before it gives up,
terminates the process, and concludes that the service failed to start.”


That's right, more precisely it's the time the SCM waits for the Service to
open it's named pipe endpoint (named something like
\Device\NamedPipe\net\NtControlPipexx).

Willy.

Nov 17 '05 #10

P: n/a
As far as the SetServiceStatus call {and most all of this stuff} goes... You
can do it with PInvoke.

First you have to call OpenServiceHandle and return an IntPtr for the
service handle, and then you have to define your own ServiceStatus struct in
C#, and then you have to initialize the struct properly {hopefully using
correct enum values that took you hours to dig out of the docs}, and then you
have to call SetServiceStatus using the handle and the struct, and then you
have to call CloseServiceHandle...

And if you screw up anywhere or throw out of the code without
disposing/closing then you will leave a full access service handle dangling
open on the SCM database and... Oh god I sooo don't miss raw WIN32
programming...

Two words: MarshalAs and StructLayout...

--Richard

"Willy Denoyette [MVP]" wrote:

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:O8**************@TK2MSFTNGP10.phx.gbl...
Frank,

You can declare the SetServiceStatus method to be called through
P/Invoke. While disposing your objects, you should make a call to
SetServiceStatus, passing a value of SERVICE_STOP_PENDING for the
dwCurrentState field of the SERVICE_STATUS structure that is passed to to
the function.

The ServiceHandle property will return the handle that you need to pass
as the first parameter to that function.

Hope this helps.


No, this isn't possible you can't get the statusHandle required to call
SetServiceStatus, this handle is private to the ServiceBase class. v2.0 of
the framework exposes the SetServiceStatus API , this solves a number of
issues we have now in v1.x .

Willy.

Nov 17 '05 #11

P: n/a
I guess you mean OpenSCManager instead of OpenServiceHandle.
Well this function returns a handle to the SCM database, but the handle
required to call SetServiceStatus is a status handle (pointer to the current
service status info structure) returned by RegisterServiceCtrlHandlerEx.
Both handles aren't the same , and can't be intermixed! (never tried
though).
The first one is used inside a SCP and is a handle used to access a specific
service DB within the SCM, while the latter is used from within a service to
report statuses to the SCM.

Willy.

"Richard" <Ri*****@discussions.microsoft.com> wrote in message
news:AE**********************************@microsof t.com...
As far as the SetServiceStatus call {and most all of this stuff} goes...
You
can do it with PInvoke.

First you have to call OpenServiceHandle and return an IntPtr for the
service handle, and then you have to define your own ServiceStatus struct
in
C#, and then you have to initialize the struct properly {hopefully using
correct enum values that took you hours to dig out of the docs}, and then
you
have to call SetServiceStatus using the handle and the struct, and then
you
have to call CloseServiceHandle...

And if you screw up anywhere or throw out of the code without
disposing/closing then you will leave a full access service handle
dangling
open on the SCM database and... Oh god I sooo don't miss raw WIN32
programming...

Two words: MarshalAs and StructLayout...

--Richard

"Willy Denoyette [MVP]" wrote:

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in
message news:O8**************@TK2MSFTNGP10.phx.gbl...
> Frank,
>
> You can declare the SetServiceStatus method to be called through
> P/Invoke. While disposing your objects, you should make a call to
> SetServiceStatus, passing a value of SERVICE_STOP_PENDING for the
> dwCurrentState field of the SERVICE_STATUS structure that is passed to
> to
> the function.
>
> The ServiceHandle property will return the handle that you need to
> pass
> as the first parameter to that function.
>
> Hope this helps.
>


No, this isn't possible you can't get the statusHandle required to call
SetServiceStatus, this handle is private to the ServiceBase class. v2.0
of
the framework exposes the SetServiceStatus API , this solves a number of
issues we have now in v1.x .

Willy.

Nov 17 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.