473,503 Members | 1,686 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Service Stop timeout setting

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
11 14929
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
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
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

"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

"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

"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

"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
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

"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1653
by: Seth | last post by:
For some reason my service works fine except that it will create the file in my c drive, but will not write to the file. Sorry if this is a duplicate post, i have found some that ask the same...
3
9051
by: Mike | last post by:
Timeout Calling Web Service I am calling a .NET 1.1 web service from an aspx page. The web service can take several minutes to complete its tasks before returning a message to the aspx page. ...
3
2596
by: zxo102 | last post by:
Hi there, I have a python application (many python scripts) and I start the application like this python myServer.py start in window. It is running in dos window. Now I would like to put it...
1
10400
by: Greg Allen | last post by:
I have a timeout issue with a long running web service, and I can't figure out where the timeout value is coming from. In the client I end up getting the following exception: The underlying...
6
8017
by: D | last post by:
I have a simple file server utility that I wish to configure as a Windows service - using the examples of the Python Win32 book, I configured a class for the service, along with the main class...
3
6598
dmjpro
by: dmjpro | last post by:
plz send me a good link which can clearify me how the J2EE framework works i want the details information .... plz help thanx
6
2481
by: half.italian | last post by:
Hi, I'm trying to serve up a simple XMLRPC server as a windows service. I got it to run properly, I'm just not sure how to stop it properly. Most of the documentation/examples I found for this...
0
2171
by: mattcfisher | last post by:
Hi, I have two windows services running together. One is the main program, and one is an "updater" wrapper for the main. The updater service starts and stops the main one (as in you should never...
10
3224
by: tshad | last post by:
I have a Windows Service that I need to put to sleep for about 10-20 minutes. The problem is that if you try to shut it down during this time, it will tell you that the service didn't respond in...
0
7087
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...
0
7281
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
7334
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
6993
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...
1
5014
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
4675
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
3168
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
3156
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
383
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...

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.