Question Folder Monitoring issue

Jose Cuervo

Member
Joined
Jul 14, 2010
Messages
14
Programming Experience
1-3
Hi,

Right i have managed to get backgroundworking working, but it has gave me an issue when i try to add it to a program i already have lol

I have an application that monitors a folder, and when a file is created it performs some actions. I have made the actions into a background worker and it works great, for a single file. If i try to run it when mutliple files hit the folder, it crashes, as it has to archive/delete the original file, and by then the variable i am using for the file name has changed.

The only way i know how to pass my file names is to create global variable (not a great idea) But i don't know what else i can do? I been stuck at this for ages lol

Here is the code i am using which does the folder monitoring, the triggers the background worker.

VB.NET:
    Dim sFileName As String
    Dim watcher As New FileSystemWatcher

    Private WithEvents BackgroundWorker As System.ComponentModel.BackgroundWorker

    Delegate Sub CallBackToUIThread(ByVal e As FileSystemEventArgs)

    'This is the section that watches for files being created in the directory
    Private Sub Window1_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded

        watcher.Path = My.Settings.folderToMonitor
        watcher.NotifyFilter = NotifyFilters.FileName

        AddHandler watcher.Created, AddressOf onChanged

        watcher.EnableRaisingEvents = True

    End Sub
    Public Sub NotifyUIThreadOfChange(ByVal e As FileSystemEventArgs)

        If e.ChangeType = WatcherChangeTypes.Created Then

            sFileName = e.FullPath
            BackgroundWorker = New System.ComponentModel.BackgroundWorker
            BackgroundWorker.WorkerReportsProgress = True
            BackgroundWorker.RunWorkerAsync()            

        End If


    End Sub

    Private Sub onChanged(ByVal source As Object, ByVal e As FileSystemEventArgs)

        'Call back to the UI thread

        Me.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, New CallBackToUIThread(AddressOf NotifyUIThreadOfChange), e)

    End Sub

In my background worker i have the line "Kill(sFileName)" which is when i am using multiple file, it all goes belly up lol

I still want the UI to be free and not lock up, but just can't figure out what to do.

Please help :(
 
Typical, when you ask you end up figuring it out! lol

Can someone give me their thoughts on my idea though?

What i done was created a new global string variable called sWorkerStatus

At the start of the background worker i set it to "Working" and at the end set it to "Free"

Then i changed the sub onChanged to have a loop in it telling it to sleep for a half second if the workerstatus is at working
VB.NET:
    Private Sub onChanged(ByVal source As Object, ByVal e As FileSystemEventArgs)

        'Call back to the UI thread

        Do
            System.Threading.Thread.Sleep(500)
            If sWorkerStatus = "Free" Then
                Exit Do

            End If
        Loop

        Me.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, New CallBackToUIThread(AddressOf NotifyUIThreadOfChange), e)

    End Sub

Actually, typing this out, i am wondering, would i be better using backgroundworker_complete to achieve it?
 
onCreated should be calling BackgroundWorker.ReportProgress().

If your program that you're calling has issues with multiple files being processed maybe you should think about sending the files to a queue where they can be processed one at a time.
 
You could add the paths to a Queue(Of String) and have BackgroundWorker process them, run worker if not IsBusy. Remember to SyncLock access to the queue object.
 
Back
Top