473,385 Members | 1,838 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Stopping Windows Service

D
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.
However, it never stops correctly (net stop returns "service could not
be stopped") and service is left in a Stopping state. My gut tells me
there's something additional I need to include in the SvcDoRun function
in order to have it shutdown gracefully, but I'm not sure what. Also,
if that is the case, where should I place the statements within the
function (i.e. right after the listen() statement?)? Thanks as
always..

Doug

Mar 1 '06 #1
6 7998
On Wed, 2006-03-01 at 12:41 -0800, D wrote:
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.
However, it never stops correctly (net stop returns "service could not
be stopped") and service is left in a Stopping state. My gut tells me
there's something additional I need to include in the SvcDoRun function
in order to have it shutdown gracefully, but I'm not sure what. Also,
if that is the case, where should I place the statements within the
function (i.e. right after the listen() statement?)? Thanks as
always..

At the risk of sounding like a geek, first problem I see is WIndows:)
Sorry couldn't help that one:) Seriously though, I would suggest that it
sounds like something is not letting the process go, which would be par
for the windows course. If you will post the snippet of code, I'm sure
we could help you a little more specifically.

Mar 1 '06 #2
D wrote:
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.
However, it never stops correctly (net stop returns "service could not
be stopped") and service is left in a Stopping state. My gut tells me
there's something additional I need to include in the SvcDoRun function
in order to have it shutdown gracefully, but I'm not sure what. Also,
if that is the case, where should I place the statements within the
function (i.e. right after the listen() statement?)? Thanks as
always..

Doug

Stopping is done by having SvcStop set the event flag so that the
next time through your loop in SvcDoRun you see that the stop has
been issued and exit your while loop. Here is some skeleton code
that I stripped from a working service. Hope it helps. Note: I
copied most of this from Mark Hammond and Andy Robinson's excellent
book Python Programming on Win32 book.

-Larry Bates

import win32serviceutil
import win32service
import win32event
import win32evtlogutil

class testService(win32serviceutil.ServiceFramework):

def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self, args)
#---------------------------------------------------------------------
# Create an event which we will use to wait on.
# The "service stop" request will set this event.
#---------------------------------------------------------------------
self.hWaitStop=win32event.CreateEvent(None, 0, 0, None)
return

def SvcStop(self):
#---------------------------------------------------------------------
# Before we do anything, tell SCM we are starting the stop process.
#---------------------------------------------------------------------
self.ReportServiceStatus(win32service.SERVICE_STOP _PENDING)
#---------------------------------------------------------------------
# And set my event
#---------------------------------------------------------------------
win32event.SetEvent(self.hWaitStop)
return

def SvcDoRun(self):
import servicemanager
#---------------------------------------------------------------------
# Make entry in the event log that this service started
#---------------------------------------------------------------------
servicemanager.LogMsg(servicemanager.EVENTLOG_INFO RMATION_TYPE,
servicemanager.PYS_SERVICE_STARTED,
(self._svc_name_, ''))

#---------------------------------------------------------------------
# Wait 10000 milliseconds (10 seconds)
#---------------------------------------------------------------------
self.timeout=10000
#-----------------------------------------------------------------
# Wait for service stop signal, if I timeout, loop again
#-----------------------------------------------------------------
rc=win32event.WaitForSingleObject(self.hWaitStop, self.timeout)
#
# Check to see if self.hWaitStop happened
#
if rc == win32event.WAIT_OBJECT_0:
#
# Stop signal encountered
#
break

#--End while--
#---------------------------------------------------------------------
# Log stopped message to EventLog
#---------------------------------------------------------------------
servicemanager.LogMsg(servicemanager.EVENTLOG_INFO RMATION_TYPE,
servicemanager.PYS_SERVICE_STOPPED,
(self._svc_name_, ''))

return
Mar 1 '06 #3
D
Thanks guys. Larry - I inserted the rc lines right after I do the
listen() and the service is not functioning properly (service is
terminating). Where am I going wrong here?

Doug

Mar 1 '06 #4
D
I must be missing something..in my SvcDoRun function, first line is
"self.timeout=10000" , followed by my "while 1:" loop which listens for
the connection, processes the data, etc. Where should I place the
"rc=win32event.." and "if rc" lines? I have a feeling I'm pretty close
here.. :) Thanks very much.

Doug

Mar 2 '06 #5
D
Sorry for the many messages, but I've been looking into it further -
since I have the server services listening and waiting for incoming
connections, how would I interrupt it to let the program know that a
stop request has been issued? For example, if I put the stop check
before it listens, I would think it'd have to wait until it loops back
around after the next client connects. If I put it after the listen, a
client would have to connect in order for it to run the check..?

Mar 2 '06 #6
D wrote:
Sorry for the many messages, but I've been looking into it further -
since I have the server services listening and waiting for incoming
connections, how would I interrupt it to let the program know that a
stop request has been issued? For example, if I put the stop check
before it listens, I would think it'd have to wait until it loops back
around after the next client connects. If I put it after the listen, a
client would have to connect in order for it to run the check..?

You are correct. Services must be "good neighbors". They
do some work and go to sleep for some period of time, then wake
up and loop. The stop signal comes while they are asleep.
When they wake up, they exit the infinite loop. If you can't
implement a listen-with-timeout it is going to be hard to make
this into a service.

-Larry Bates
Mar 3 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: CG | last post by:
Hi I have developed a Windows Service When I try to start the Service it tells me that it cannot start as there may not be any work to do When I comment out below line of code in my OnStart...
0
by: Daniel O'Brien | last post by:
Hi - any help with this would be greatly appreicated - it has already had me confused for a good few hours! I am using Visual Studio 2003 and the .NET framework 1.1. I have a C# Windows...
4
by: Keith | last post by:
I'm in the same boat as the fellow who posted this message back in August: Title : Windows Service, How does one make a service "fail" properly? Author : Ross Bennett Group :...
0
by: GTS | last post by:
Hi, I have created my application to run as windows service. It is developed using VC++ on Windows 2000 platform. It is working fine with Windows 2000. But I am seeing problems in Windows 2003...
7
by: Gene | last post by:
I have a Windows Service (VB.NET) that runs 24/7. It queries a Web service 5 to 10 times per hour. About 2 or 3 times a month, it fails. The log indicates that it sends the request to the Web...
0
by: news.microsoft.com | last post by:
Hi, I have a windows service that stops, but in the control panel/services it says that it is running. When I try to stop it, it times out, and just says 'stopping' as its state. I have to kill...
0
by: Glen Wolinsky | last post by:
I am creating a Windows service that will check a request queue (database) for pending requests. It will then process each individual request until completed, wait a set time interval and then...
5
by: chris.hearson | last post by:
How do I programmatically prevent a service from stopping? I want to be able to keep my service running (started), under certain conditions, when a user tries to stop it. I have tried throwing an...
10
by: archana | last post by:
Hi all, I am having one windows service which is updating to database. On 'Onstop i want to wait till current updation complete. How will i do this? Because if i write some lengthy code on...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
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,...
0
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
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
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...

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.