|
-
Sep 12th, 2008, 11:40 AM
#1
Thread Starter
Lively Member
[RESOLVED] [2005] Running the handler once on OnStart of a Service
I've put off posting this one for a while, as I know related subjects hav been discussed, but I just can't seem to get it working.
I have a service with a FileSystemWatcher:
Code:
Public Class RightHemisphereLoader
Private watcher As IO.FileSystemWatcher
Private zipfile As ICSharpCode.SharpZipLib.Zip.ZipFile
Private utils As New RHUtils.RHUtils
Protected Overrides Sub OnStart(ByVal args() As String)
Try
' Add code here to start your service. This method should set things
' in motion so your service can do its work.
'At this point we've processed what's there.
'Set up our watcher for LCA data
watcher = New IO.FileSystemWatcher
watcher.Path = utils.setting("InputDirectory")
watcher.Filter = "*.zip"
watcher.IncludeSubdirectories = True
watcher.NotifyFilter = NotifyFilters.FileName
AddHandler watcher.Created, AddressOf loadRHviaDirectoryWatcher
'Begin watching
watcher.EnableRaisingEvents = True
utils.WriteLog("Service and directory watcher started.", "OnStart", "Info")
'In case there's stuff there, clean and raise the event
cleanDir(utils.setting("WorkingDirectory"))
Catch ex As Exception
utils.WriteLog(ex.Message(), "OnStart", "ERROR")
End Try
End Sub
What I'm trying to do is call the loadRHviaDirectoryWatcher once to process any files that are already there. Trouble is, that processing often takes hours, and OnStart has to finish in 30 seconds or the service fails to start.
I've tried creating a custom event that loadRHviaDirectoryWatcher handles and raising it, but then the AddHandler line complains.
How is this best accomplished? Thanks!
-
Sep 12th, 2008, 01:22 PM
#2
Re: [2005] Running the handler once on OnStart of a Service
I'd probably do it by spinning up a timer at the end of the OnStart... then when the timer expires, call the method to process existing files.
-tg
-
Sep 12th, 2008, 01:37 PM
#3
Re: [2005] Running the handler once on OnStart of a Service
if processing takes hours, why not offload all the processing to seperate threads?
-
Sep 15th, 2008, 09:28 AM
#4
Thread Starter
Lively Member
Re: [2005] Running the handler once on OnStart of a Service
Thanks for the suggestions.
I'll try the timer idea.
I don't want to thread it because if multiple files process at once there are issues due to the nature of the data I'm processing.
-
Sep 15th, 2008, 10:46 AM
#5
Thread Starter
Lively Member
Re: [2005] Running the handler once on OnStart of a Service
Got it. Threading the function did work, but I had to make a wrapper function for the directory watcher.
Code:
Protected Overrides Sub OnStart(ByVal args() As String)
'End Sub
'Public Sub OnStart2()
Try
' Add code here to start your service. This method should set things
' in motion so your service can do its work.
Dim t As Threading.Thread
Dim tm As New ThreadMethods
'At this point we've processed what's there.
'Set up our watcher for LCA data
watcher = New IO.FileSystemWatcher
watcher.Path = utils.setting("InputDirectory")
watcher.Filter = "*.zip"
watcher.IncludeSubdirectories = True
watcher.NotifyFilter = NotifyFilters.FileName
AddHandler watcher.Created, AddressOf tm.loadRHviaDirectoryWatcherWrapper
'Begin watching
watcher.EnableRaisingEvents = True
utils.WriteLog("Service and directory watcher started.", "OnStart", "Info")
'In case there's stuff there, clean and raise the event
tm.watcher = watcher
tm.utils = utils
tm.cleanDir(utils.setting("WorkingDirectory"))
'Fork the process
t = New Threading.Thread(AddressOf tm.loadRHviaDirectoryWatcher)
t.Start()
Catch ex As Exception
utils.WriteLog(ex.Message(), "OnStart", "ERROR")
End Try
End Sub
Public Class ThreadMethods
Public watcher As IO.FileSystemWatcher
Public utils As RHUtils.RHUtils
'Wrapper to handle the stupid source/e thing
Public Sub loadRHviaDirectoryWatcherWrapper(ByVal source As Object, ByVal e As FileSystemEventArgs)
loadRHviaDirectoryWatcher()
End Sub
'This gets called when a new zip file lands in our watched directory
Public Sub loadRHviaDirectoryWatcher()
Try
'Stop the watcher
watcher.EnableRaisingEvents = False
'Grab last file.
Dim nextFileName = getNextFileName(utils.setting("InputDirectory"))
If (nextFileName = "0") Then
Exit Sub
End If
'Wait until we're sure the FTP is done
While utils.IsFileInUse(utils.setting("InputDirectory") & nextFileName)
System.Threading.Thread.Sleep(1000) ' Sleep for 1 second
End While
'Process the File
processZipFile(nextFileName)
'Check for more files that may have shown up in the meantime and process in order
nextFileName = getNextFileName(utils.setting("InputDirectory"))
While (nextFileName <> "0")
processZipFile(nextFileName)
nextFileName = getNextFileName(utils.setting("InputDirectory"))
End While
'At this point the directory is empty, so we can restart the watcher
watcher.EnableRaisingEvents = True
Catch ex As Exception
utils.WriteLog(ex.Message(), "loadRHviaDirectoryWatcher", "ERROR")
End Try
End Sub
End Class
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|