473,406 Members | 2,847 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,406 software developers and data experts.

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

Dim MyKey As RegistryKey =
Registry.LocalMachine.OpenSubKey("Software\\Delete Folders", 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
Jul 16 '07 #1
5 2182

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.microsoft.comwrote in message
news:CE**********************************@microsof t.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 strFoldersFromRegistry As String
Dim strIntervalFromRegistry As String

Dim MyKey As RegistryKey =
Registry.LocalMachine.OpenSubKey("Software\\Delete Folders", 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
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 strFoldersFromRegistry As String
Dim strIntervalFromRegistry As String

Dim MyKey As RegistryKey =
Registry.LocalMachine.OpenSubKey("Software\\Delete Folders", 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?
Jul 16 '07 #3
On Jul 16, 7:46 am, Jonathan Smith
<JonathanSm...@discussions.microsoft.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 strFoldersFromRegistry As String
Dim strIntervalFromRegistry As String

Dim MyKey As RegistryKey =
Registry.LocalMachine.OpenSubKey("Software\\Delete Folders", 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

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
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...
2
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...
2
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...
23
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...
8
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...
9
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
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
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...
0
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:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
0
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,...
0
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...

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.