473,625 Members | 2,677 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why won't my service stop?

I have written a service, but it won't stop, Eventvwr reports the following:

"Failed to stop service"

The code is as follows:
=============== =============== ==============
Protected Overrides Sub OnStart(ByVal args() As String)
' Add code here to start your service. This method should set things
' in motion so your service can do its work.

Dim strFoldersFromR egistry As String
Dim strIntervalFrom Registry As String

Dim MyKey As RegistryKey =
Registry.LocalM achine.OpenSubK ey("Software\\D eleteFolders", True)

strFoldersFromR egistry = MyKey.GetValue( "FolderList ")
strIntervalFrom Registry = MyKey.GetValue( "Interval")

'Split the folder list into an array of individual entries
strFolderList = Split(strFolder sFromRegistry, ";")

'Start the timer and set the interval to the correct number of
milliseconds
Dim dblTimerInterva l As Double

dblTimerInterva l = CDbl(strInterva lFromRegistry)

Timer1.Interval = (dblTimerInterv al * 1000)
Timer1.Start()

EventLog1.Write Entry("Delete Folders", "DeleteFold ers is now
monitoring " & strFoldersFromR egistry)

End Sub
=============== =============== =============== =======
Protected Overrides Sub OnStop()
' Add code here to perform any tear-down necessary to stop your
service.

booRunning = False

'Stop the timer
Timer1.Stop()

EventLog1.Write Entry("DeleteFo lders is no longer monitoring folders")

End Sub
=============== =============== =============== ======
Private Sub Timer1_Elapsed( ByVal sender As System.Object, ByVal e As
System.Timers.E lapsedEventArgs ) Handles Timer1.Elapsed

Do While booRunning = True
'Do the "Delete Folders" routine for each folder in the list
Dim strCurrentFolde r As String

Dim MyFolder As New FileSystemObjec t

For Each strCurrentFolde r In strFolderList

If Len(strCurrentF older) = 0 Then Exit Sub

'Check the folder exists
If MyFolder.Folder Exists(strCurre ntFolder) = True Then

DeleteEmptyFold ers(strCurrentF older)
End If

Next

Loop
End Sub
=============== =============== =============== =======
Private Sub DeleteEmptyFold ers(ByVal strCurrentFolde r As String)
Dim fsoSubFolders As Folders
Dim fsoSubFolder As Folder
Dim fsoFolder As Folder

Dim strPaths()
Dim lngFolder As Long
Dim lngSubFolder As Long

Dim m_fsoObject = New FileSystemObjec t
If Not m_fsoObject.Fol derExists(strCu rrentFolder) Then Exit Sub

fsoFolder = m_fsoObject.Get Folder(strCurre ntFolder)

On Error Resume Next

'Has sub-folders
If fsoFolder.SubFo lders.Count 0 Then
lngFolder = 1
ReDim strPaths(fsoFol der.SubFolders. Count)
'Get each sub-folders path and add to an array
For Each fsoSubFolder In fsoFolder.SubFo lders
strPaths(lngFol der) = fsoSubFolder.Pa th
lngFolder = lngFolder + 1
Next fsoSubFolder

lngSubFolder = 1
'Recursively call the function for each sub-folder
Do While lngSubFolder < lngFolder
Call DeleteEmptyFold ers(strPaths(ln gSubFolder))
lngSubFolder = lngSubFolder + 1
Loop
End If

'No sub-folders or files
If fsoFolder.Files .Count = 0 And fsoFolder.SubFo lders.Count = 0 Then
fsoFolder.Delet e()
End If

End Sub
Jul 16 '07 #1
5 2198

You don't have any error handling anywhere where to be found, so if
something blew you wouldn't even know it. If it won't stop, then in most
cases I have seen, it's blown up.
"Jonathan Smith" <Jo***********@ discussions.mic rosoft.comwrote in message
news:CE******** *************** ***********@mic rosoft.com...
>I have written a service, but it won't stop, Eventvwr reports the
following:

"Failed to stop service"

The code is as follows:
=============== =============== ==============
Protected Overrides Sub OnStart(ByVal args() As String)
' Add code here to start your service. This method should set
things
' in motion so your service can do its work.

Dim strFoldersFromR egistry As String
Dim strIntervalFrom Registry As String

Dim MyKey As RegistryKey =
Registry.LocalM achine.OpenSubK ey("Software\\D eleteFolders", True)

strFoldersFromR egistry = MyKey.GetValue( "FolderList ")
strIntervalFrom Registry = MyKey.GetValue( "Interval")

'Split the folder list into an array of individual entries
strFolderList = Split(strFolder sFromRegistry, ";")

'Start the timer and set the interval to the correct number of
milliseconds
Dim dblTimerInterva l As Double

dblTimerInterva l = CDbl(strInterva lFromRegistry)

Timer1.Interval = (dblTimerInterv al * 1000)
Timer1.Start()

EventLog1.Write Entry("Delete Folders", "DeleteFold ers is now
monitoring " & strFoldersFromR egistry)

End Sub
=============== =============== =============== =======
Protected Overrides Sub OnStop()
' Add code here to perform any tear-down necessary to stop your
service.

booRunning = False

'Stop the timer
Timer1.Stop()

EventLog1.Write Entry("DeleteFo lders is no longer monitoring
folders")

End Sub
=============== =============== =============== ======
Private Sub Timer1_Elapsed( ByVal sender As System.Object, ByVal e As
System.Timers.E lapsedEventArgs ) Handles Timer1.Elapsed

Do While booRunning = True
'Do the "Delete Folders" routine for each folder in the list
Dim strCurrentFolde r As String

Dim MyFolder As New FileSystemObjec t

For Each strCurrentFolde r In strFolderList

If Len(strCurrentF older) = 0 Then Exit Sub

'Check the folder exists
If MyFolder.Folder Exists(strCurre ntFolder) = True Then

DeleteEmptyFold ers(strCurrentF older)
End If

Next

Loop
End Sub
=============== =============== =============== =======
Private Sub DeleteEmptyFold ers(ByVal strCurrentFolde r As String)
Dim fsoSubFolders As Folders
Dim fsoSubFolder As Folder
Dim fsoFolder As Folder

Dim strPaths()
Dim lngFolder As Long
Dim lngSubFolder As Long

Dim m_fsoObject = New FileSystemObjec t
If Not m_fsoObject.Fol derExists(strCu rrentFolder) Then Exit Sub

fsoFolder = m_fsoObject.Get Folder(strCurre ntFolder)

On Error Resume Next

'Has sub-folders
If fsoFolder.SubFo lders.Count 0 Then
lngFolder = 1
ReDim strPaths(fsoFol der.SubFolders. Count)
'Get each sub-folders path and add to an array
For Each fsoSubFolder In fsoFolder.SubFo lders
strPaths(lngFol der) = fsoSubFolder.Pa th
lngFolder = lngFolder + 1
Next fsoSubFolder

lngSubFolder = 1
'Recursively call the function for each sub-folder
Do While lngSubFolder < lngFolder
Call DeleteEmptyFold ers(strPaths(ln gSubFolder))
lngSubFolder = lngSubFolder + 1
Loop
End If

'No sub-folders or files
If fsoFolder.Files .Count = 0 And fsoFolder.SubFo lders.Count = 0
Then
fsoFolder.Delet e()
End If

End Sub
Jul 16 '07 #2
I have now simplified the code as:

Protected Overrides Sub OnStart(ByVal args() As String)
' Add code here to start your service. This method should set things
' in motion so your service can do its work.

Dim strFoldersFromR egistry As String
Dim strIntervalFrom Registry As String

Dim MyKey As RegistryKey =
Registry.LocalM achine.OpenSubK ey("Software\\D eleteFolders", True)

strFoldersFromR egistry = MyKey.GetValue( "FolderList ")
strIntervalFrom Registry = MyKey.GetValue( "Interval")

'Split the folder list into an array of individual entries
strFolderList = Split(strFolder sFromRegistry, ";")

'Start the timer and set the interval to the correct number of
milliseconds
Dim dblTimerInterva l As Double

dblTimerInterva l = CDbl(strInterva lFromRegistry)

Timer1.Interval = (dblTimerInterv al * 1000)
Timer1.Enabled = True

EventLog1.Write Entry("Delete Folders", "DeleteFold ers is now
monitoring " & strFoldersFromR egistry)

End Sub
=============== =============== =============== ======
Protected Overrides Sub OnStop()
' Add code here to perform any tear-down necessary to stop your
service.

'Stop the timer
Timer1.Enabled = False

EventLog1.Write Entry("DeleteFo lders is no longer monitoring folders")

End Sub
=============== =============== =============== =====
Private Sub Timer1_Elapsed( ByVal sender As System.Object, ByVal e As
System.Timers.E lapsedEventArgs ) Handles Timer1.Elapsed

EventLog1.Write Entry("DeleteFo lders", CStr(Len(strFol derList)))

End Sub
I know the OnStart bit is working fine as the eventlog messages are being
reported correctly. But the service will still not stop.

Any ideas?
Jul 16 '07 #3
On Jul 16, 7:46 am, Jonathan Smith
<JonathanSm...@ discussions.mic rosoft.comwrote :
I have now simplified the code as:

Protected Overrides Sub OnStart(ByVal args() As String)
' Add code here to start your service. This method should set things
' in motion so your service can do its work.

Dim strFoldersFromR egistry As String
Dim strIntervalFrom Registry As String

Dim MyKey As RegistryKey =
Registry.LocalM achine.OpenSubK ey("Software\\D eleteFolders", True)

strFoldersFromR egistry = MyKey.GetValue( "FolderList ")
strIntervalFrom Registry = MyKey.GetValue( "Interval")

'Split the folder list into an array of individual entries
strFolderList = Split(strFolder sFromRegistry, ";")

'Start the timer and set the interval to the correct number of
milliseconds
Dim dblTimerInterva l As Double

dblTimerInterva l = CDbl(strInterva lFromRegistry)

Timer1.Interval = (dblTimerInterv al * 1000)
Timer1.Enabled = True

EventLog1.Write Entry("Delete Folders", "DeleteFold ers is now
monitoring " & strFoldersFromR egistry)

End Sub
=============== =============== =============== ======
Protected Overrides Sub OnStop()
' Add code here to perform any tear-down necessary to stop your
service.

'Stop the timer
Timer1.Enabled = False

EventLog1.Write Entry("DeleteFo lders is no longer monitoring folders")

End Sub
=============== =============== =============== =====
Private Sub Timer1_Elapsed( ByVal sender As System.Object, ByVal e As
System.Timers.E lapsedEventArgs ) Handles Timer1.Elapsed

EventLog1.Write Entry("DeleteFo lders", CStr(Len(strFol derList)))

End Sub

I know the OnStart bit is working fine as the eventlog messages are being
reported correctly. But the service will still not stop.

Any ideas?
Have you tried Disposing of the timer? I'm not sure if that will
help, but it couldn't hurt.

Chris

Jul 16 '07 #4

<snipped>
>
I know the OnStart bit is working fine as the eventlog messages are being
reported correctly. But the service will still not stop.

Any ideas?
It's called debug it. You should start dumping message into the eventlog
about code execution and where it's at in the code when it's not stopping.

Or you can start using the NetSend command sending the messages to the
machine name as to where you're at in the code for possible area/code
location for a possible pin point. You'll have to enable the NT based O/S's
Messenger service to have the messages displayed to you, if using the
Netsend.

No one here can debug your solution for you. That's something you're going
to have to do yourself.

Jul 16 '07 #5
Definitely need to debug the app.

I would also remove the on error resume next statement and replace it
with a try catch statement, this way if an error is thrown your can
handled if accordingly, ie write and entry to the eventlog.

I can see several scenarios where your application could run infinite
loops in the same directory. For example, lets say you have c:\test
with a sub directory of c:\test\sub. c:\test\sub contains a file
mydll.dll that is currently being used by the operating system or some
other application, and your service can not delete it. In this case,
and error will be throw, but your on error resume next will cause your
method to continually try to delete the file over and over again,
cause the service to hang.

There are several more scenarios, but this is my initial intuirion.

Kr3at
Developers, List your services.
http://directory.kr3at.com

Jul 17 '07 #6

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

Similar topics

1
363
by: Peter Steele | last post by:
Okay, I assume I'm missing something obvious here. I have created a simple service in C# that on starting spawns a thread to do some processing. The service can be stopped with a "net stop" command of course, but under some circumstances the service will decide to terminate itself. My OnStart looks something like this: protected override void OnStart(string args) { serviceThread = new Thread(new ThreadStart(ServiceThreadStart));...
2
5281
by: 0to60 | last post by:
I have a windows service that when started creates two threads: one thread that runs a TcpListener waiting for TcpClients to connect, and another thread that reads from the resulting sockets. My stop code .Abort()s the threads and .Stop()s the listener. One one of my computers, the service runs like, well...a service. It starts and stops nicely. On a different computer (eventually this service will be running on 50+ machines) the...
2
2839
by: Prasad | last post by:
Hi, I am writing a service which takes a long time to stop after the OnStop call is given by the Services Snap-in. The problem is I cannot cut down on the time that it takes to Stop. The Service snap-in gives me the error saying that the service did not respond to the Stop call in a timely fashion. So is there any method by which I can get around this problem. Thanks Prasad
23
7574
by: Adam Clauss | last post by:
I have a C# Windows Service running as the NetworkService account because it needs to access a network share. As part of the service's initialization, I want the service to terminate, if an unrecoverable error occurs. When that case occurs, I create a ServiceController object and call the Stop() method. However - I get an exception thrown saying access denied. If I switch to using the LocalService account it works fine, but I lose...
8
1421
by: Jerry Camel | last post by:
I want my service to terminate automatically if the specified parameters are invalid. I tried to use a servicecontroller component to attach to the service, but I think that it's failing because it's being called in the OnStart event (where I validate the parameters) and the service isn't fully running yet. How can I stop my service from within the service itself? Thanks. Jerry
9
1553
by: Thirsty Traveler | last post by:
I dropped a Timer object onto a Windows Service and set enabled=true... however, it does not appear to be firing. Do I need to do something else?
4
3174
by: kkt49 | last post by:
# vim: et sw=4 ts=8 sts from wxPython.wx import * import sys, os, time import pywintypes import win32serviceutil import win32service import win32event import win32process
7
7221
by: shai | last post by:
I am working at .net 1.1, writing in c#. I have windows service with a COM object. Every unexpected time The COM object throw an error that make my service get stuck (do not respond). I can catch this error. I want to restart my windows service every time the COM object throws an error. I use System.ServiceProcess.ServiceController to stop and start my service. But there is one thing I do not understand:
0
1317
by: =?Utf-8?B?anAybXNmdA==?= | last post by:
I have a windows application that does not stop running whenever the application exits. Could someone fill me in on what I am doing wrong? Here is the relevant code: ================================= Private m_thTCP As Thread Private m_listener As TcpListener
0
8189
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8692
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8635
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8354
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8497
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7182
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 project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6116
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4089
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1499
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.