473,520 Members | 3,453 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 14934
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
1654
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 question - but noone seems to answer. Do i need to do anything specific to write to a text file from a windows service, much appreciated.
3
9052
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. If the web service is taking a long time to complete, the aspx page returns a ‘The operation has timed-out.’ Message to the web browser after...
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 in background as NT service. I got a example code: SmallestService.py from chapter 18 of the book "Python Programming On Win32" by Mark Hammond etc....
1
10411
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 connection was closed: An unexpected error occurred on a receive. at System.Web.Services.Protocols.WebClientProtocol.GetWebResponse(WebRequest...
6
8020
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 functions __init__, SvcStop, and SvcDoRun (which contains my server code). After registering the service, I am able to start it with no problems....
3
6600
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
2483
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 was from forums, so I'd love some links to relevant info also. Here's what I have...taken from the cookbook with the xmlrpc server added: import...
0
2173
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 start or stop main service manually, only updater). On most computers it works great, the updater's onStop() can start and stop the main service...
10
3225
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 time and end up in a "Stopping" state and there is nothing you can do until you reboot the system. What I did to solve the problem was just set a...
0
7315
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main...
0
7225
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7184
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7582
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5759
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5145
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 presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3288
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
847
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
516
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.