Results 1 to 5 of 5

Thread: [RESOLVED] [2005] Running the handler once on OnStart of a Service

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2007
    Posts
    89

    Resolved [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!

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [2005] Running the handler once on OnStart of a Service

    if processing takes hours, why not offload all the processing to seperate threads?

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Dec 2007
    Posts
    89

    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.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Dec 2007
    Posts
    89

    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
  •  



Click Here to Expand Forum to Full Width