Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more: , +
Hello
I'm a beginner

I need help how to implement an console with filesystemwatcher that capture created files and another thread (process) that move those files on intranet (with a slower connection)

I'm Lost on this code:

1 pass:
FSW capture files and added those files on Arraylist


2 pass:
if this Arraylist contain data then start the RunOCR procedure




<pre lang="vb">
Public Class FSWClient
        Public Event CiSonoFile(ByVal Sender As Object, ByVal e As System.EventArgs)
         Dim myFSW As New Thread(AddressOf LanciaFSW)
        Public Sub New()
            Dim watcher As New FileSystemWatcher()
            watcher.Path = "c:\test"
            watcher.IncludeSubdirectories = True
            watcher.NotifyFilter = (NotifyFilters.LastAccess Or NotifyFilters.LastWrite Or NotifyFilters.FileName Or NotifyFilters.DirectoryName)
            watcher.Filter = "*.tif"
            AddHandler watcher.Created, AddressOf OnChanged
            watcher.EnableRaisingEvents = True
            'Dim Thread1 As New System.Threading.Thread(AddressOf counter.Count)
            myFSW.Start()
            ' Wait for the user to quit the program.
            Console.WriteLine("Monitoring " & "c:\test")
            Console.WriteLine("Press 'q' to quit this console.")
            While Chr(Console.Read()) <> "q"c
            End While
        End Sub


        Sub LanciaFSW()
            Dim oFSWClient As New FSWClient
            AddHandler oFSWClient.CiSonoFile, AddressOf GoWithOCR
        End Sub


        Dim OCRisRunning As Boolean = False

        Sub GoWithOCR()
            If PathFilesList.Count > 0 AndAlso OCRisRunning = False Then
                RunOCR()
            End If
        End Sub

        Private Sub OnChanged(ByVal source As Object, ByVal e As FileSystemEventArgs)
            ' Specify what is done when a file is changed, created, or deleted.
            If e.ChangeType = IO.WatcherChangeTypes.Created Then
                PathFilesList.Add(e.FullPath)
                RaiseEvent CiSonoFile(Me, New EventArgs)
            End If
        End Sub


        Private Sub RunOCR()
            Dim a As Integer
            Dim fileinput, fileoutput As String
            Do While PathFilesList.Count 

                fileinput = PathFilesList.Item(a)
                Dim filein As New FileInfo(fileinput)
                fileoutput = "d:\test\" & filein.Name
                CopyFiles(fileinput, fileoutput)
                PathFilesList.Remove(fileinput)

            Loop
        End Sub


        Private Sub CopyFiles(ByVal filein As String, ByVal fileout As String)

            File.Copy(filein, fileout)

        End Sub

    End Class



Any help is appreciated.
Posted
Updated 5-Jan-11 22:42pm
v2
Comments
Rajesh Anuhya 6-Jan-11 4:45am    
what is your problem??
la_morte 6-Jan-11 5:25am    
My code is not complete. i need to correct it.

1 solution

I'm not entirely sure what you are asking for but here is an extensive article about FileSystemWatcher
FileSystemWatcher - Pure Chaos (Part 1 of 2)[^]

Remeber to protect PathFilesList from concurrency issues

Regards
Espen Harlinn
 
Share this answer
 
Comments
la_morte 6-Jan-11 5:26am    
is very complicated for me this article. I need only the code to correct my code. (I'm new on programming and multi-threating)
Espen Harlinn 6-Jan-11 5:35am    
If you are new to programming, multithreading and concurrency issues requires something like a couple of books, and some trial and error to get right. It's usually not a good idea to try to assimilate everthing at one time. Doing what I think you are attempting to do here in a correct and robust manner takes time, more time than I can possibly justify on a free quick answer. Good luck

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900