Charora's website  
Home arrow Technical Articles arrow Microsoft.NET arrow Windows Services arrow Killing a hang on Windows Service - "Stopping"
Saturday, 04 September 2010
 
 
Killing a hang on Windows Service - "Stopping" PDF Print E-mail
Written by Administrator   
Thursday, 31 August 2006
It sometimes happens and it is not a good sign most of the time, you'd like to stop a Windows Service, and when you issue the stop command through the SCM (Service Control Manager) or by using the ServiceProcess classes in the .NET Framework or by other means (net stop, Win32 API), the service remains in the state of "stopping" and never reaches the stopped phase.

It's pretty simple to simulate this behavior by creating a Windows Service in C# (or any .NET language whatsoever) and adding an infinite loop in the Stop method.

The only way to stop the service is by killing the process then.

However, sometimes it is not clear what the process name or ID is (e.g. when you're running a service hosting application that can cope with multiple instances such as SQL Server Notification Services).

The way to do it is as follows:

Go to the command-prompt and query the service (e.g. the SMTP service) by using sc:

sc queryex SMTPSvc
This will give you the following information:
SERVICE_NAME: SMTPSvc
TYPE : 20 WIN32_SHARE_PROCESS
STATE : 4 RUNNING
(STOPPABLE, PAUSABLE, ACCEPTS_SHUTDOWN)
WIN32_EXIT_CODE : 0 (0x0)
SERVICE_EXIT_CODE : 0 (0x0)
CHECKPOINT : 0x0
WAIT_HINT : 0x0
PID : 388
FLAGS :

or something like this (the "state" will mention stopping).

You can find the process identifier (PID) over here, so it's pretty easy to kill the associated process either by using the task manager or by using taskkill:

taskkill /PID 388 /F

where the /F flag is to force to kill the process. First try without the flag.

Please be careful when you do this; it is useful for emergencies but you shouldn't use it on a regular basis (use it as a last option to solve the problem or to avoid the need of a reboot in an exceptional situation). It can even be used to stop a service that has the "NOT-STOPPABLE" and/or "IGNORES_SHUTDOWN" flag set (e.g. Terminal Services on a Windows Server 2003 is non-stoppable), at least when it's not hosted in the system process. You can query all this information using the sc command.

More info on sc.exe can be found via http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/sc.mspx.

Comments
NA
Written by on 2010-07-06 07:14:03
Thank you, I have had this problem so many times, andthe only solution I knew was to reboot the PC! Your a time saver- keep up the good work..

Write Comment
  • Please keep the topic of messages relevant to the subject of the article.
  • Personal verbal attacks will be deleted.
  • Please don't use comments to plug your web site. Such material will be removed.
  • Just ensure to *Refresh* your browser for a new security code to be displayed prior to clicking on the 'Send' button.
  • Keep in mind that the above process only applies if you simply entered the wrong security code.
Name:
E-mail
Homepage
Title:
BBCode:Web AddressE-mailBold TextItalic TextUnderlined TextQuoteCodeOpen ListList ItemClose List
Comment:



Code:* Code:

Last Updated ( Tuesday, 06 November 2007 )
 
Next >
Google





Lost Password?
No account yet? Register
Archive
Newsflash

The six most important words:
"I admit I made a mistake."
The five most important words:
"You did a good job."
The four most important words:
"What is your opinion."
The three most important words:
"If you please."
The two most important words:
"Thank you,"
The one most important word:
"We"
The least most important word:
"I"
-
Author unknown

 
Related Items
Sections
 
Top! Top!