Why won't my service stop?


G

Guest

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 strFoldersFromRegistry As String
Dim strIntervalFromRegistry As String

Dim MyKey As RegistryKey =
Registry.LocalMachine.OpenSubKey("Software\\DeleteFolders", True)

strFoldersFromRegistry = MyKey.GetValue("FolderList")
strIntervalFromRegistry = MyKey.GetValue("Interval")

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

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

dblTimerInterval = CDbl(strIntervalFromRegistry)

Timer1.Interval = (dblTimerInterval * 1000)
Timer1.Start()

EventLog1.WriteEntry("Delete Folders", "DeleteFolders is now
monitoring " & strFoldersFromRegistry)

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.WriteEntry("DeleteFolders is no longer monitoring folders")

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

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

Dim MyFolder As New FileSystemObject

For Each strCurrentFolder In strFolderList

If Len(strCurrentFolder) = 0 Then Exit Sub

'Check the folder exists
If MyFolder.FolderExists(strCurrentFolder) = True Then

DeleteEmptyFolders(strCurrentFolder)
End If

Next

Loop
End Sub
====================================================
Private Sub DeleteEmptyFolders(ByVal strCurrentFolder 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 FileSystemObject
If Not m_fsoObject.FolderExists(strCurrentFolder) Then Exit Sub

fsoFolder = m_fsoObject.GetFolder(strCurrentFolder)

On Error Resume Next

'Has sub-folders
If fsoFolder.SubFolders.Count > 0 Then
lngFolder = 1
ReDim strPaths(fsoFolder.SubFolders.Count)
'Get each sub-folders path and add to an array
For Each fsoSubFolder In fsoFolder.SubFolders
strPaths(lngFolder) = fsoSubFolder.Path
lngFolder = lngFolder + 1
Next fsoSubFolder

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

'No sub-folders or files
If fsoFolder.Files.Count = 0 And fsoFolder.SubFolders.Count = 0 Then
fsoFolder.Delete()
End If

End Sub
 
Ad

Advertisements

M

Mr. Arnold

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.
 
G

Guest

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 strFoldersFromRegistry As String
Dim strIntervalFromRegistry As String

Dim MyKey As RegistryKey =
Registry.LocalMachine.OpenSubKey("Software\\DeleteFolders", True)

strFoldersFromRegistry = MyKey.GetValue("FolderList")
strIntervalFromRegistry = MyKey.GetValue("Interval")

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

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

dblTimerInterval = CDbl(strIntervalFromRegistry)

Timer1.Interval = (dblTimerInterval * 1000)
Timer1.Enabled = True

EventLog1.WriteEntry("Delete Folders", "DeleteFolders is now
monitoring " & strFoldersFromRegistry)

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.WriteEntry("DeleteFolders is no longer monitoring folders")

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

EventLog1.WriteEntry("DeleteFolders", CStr(Len(strFolderList)))

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?
 
C

Chris Dunaway

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 strFoldersFromRegistry As String
Dim strIntervalFromRegistry As String

Dim MyKey As RegistryKey =
Registry.LocalMachine.OpenSubKey("Software\\DeleteFolders", True)

strFoldersFromRegistry = MyKey.GetValue("FolderList")
strIntervalFromRegistry = MyKey.GetValue("Interval")

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

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

dblTimerInterval = CDbl(strIntervalFromRegistry)

Timer1.Interval = (dblTimerInterval * 1000)
Timer1.Enabled = True

EventLog1.WriteEntry("Delete Folders", "DeleteFolders is now
monitoring " & strFoldersFromRegistry)

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.WriteEntry("DeleteFolders is no longer monitoring folders")

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

EventLog1.WriteEntry("DeleteFolders", CStr(Len(strFolderList)))

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
 
M

Mr. Arnold

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.
 
Ad

Advertisements

K

Kr3at

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
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top