0

I am trying to make a CheckedListBox_Notifications that shows items listing the changes happening in a specific directory. Upon the load of the form I have the code:

    Public Watcher_ActiveProjects As New FileSystemWatcher()

    Sub Form_Home_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        'Watcher_ActiveProjects
        Watcher_ActiveProjects = New FileSystemWatcher() With {
        .Path = Directory_ActiveProjects,
        .IncludeSubdirectories = True,
        .NotifyFilter = NotifyFilters.FileName Or NotifyFilters.DirectoryName Or NotifyFilters.LastWrite,
        .EnableRaisingEvents = True
    }

        AddHandler Watcher_ActiveProjects.Created, AddressOf oncreate
    end sub

And so upon "OnCreate" I am adding the item using the code:

    Public Sub oncreate(sender As Object, e As FileSystemEventArgs)

        'Set Variables
        strFullPath = e.FullPath
        strProjectSuffix = Split(strFullPath, "\")(3)

        Form_Home.CheckedListBox_Notifications.Items.Add("File created:")

    End Sub

The problem is that using OnCreate sub it doesn't update the CheckedListBox_Notifications. I hear that it might be related to threading? Could someone, please, help me with this one?

1 Answer 1

4

A FileSystemWatcher raises its events on a background thread by default. You are using the default instance of Form_Home in your event handler and default instances are thread-specific. That means that your code is working properly but it is populating a control on a different form that you can't see, rather than the one you're looking at.

There are various ways you could address this but, in your case, the simplest option is to set the SynchronizingObject property of the FileSystemWatcher - just assign the current form, i.e. Me - and then the events will be raised on the UI thread. You can then access controls on the current form directly.

I would also recommend that you simplify your code by declaring your field WithEvents and then use Handles rather than AddHandler.

Private WithEvents activeProjectsWatcher As New FileSystemWatcher With {
        .Path = Directory_ActiveProjects,
        .IncludeSubdirectories = True,
        .NotifyFilter = NotifyFilters.FileName Or NotifyFilters.DirectoryName Or NotifyFilters.LastWrite,
        .EnableRaisingEvents = True,
        .SynchronizingObject = Me
    }

Private Sub ActiveProjectsWatcher_Created(sender As Object, e As EventArgs) Handles ActiveProjectsWatcher.Created
    '...

    CheckedListBox_Notifications.Items.Add("File created:")
End Sub

Having said that, if you are doing some long-running work other than what you've shown us, it would be more appropriate to do that on a background thread and then only do the UI update on the UI thread.

Sign up to request clarification or add additional context in comments.

3 Comments

Ok so I guess that I actually need it to be ```` Public WithEvents Watcher_ActiveProjects As New FileSystemWatcher With { .Path = Directory_ActiveProjects, .IncludeSubdirectories = True, .NotifyFilter = NotifyFilters.FileName Or NotifyFilters.DirectoryName Or NotifyFilters.LastWrite, .EnableRaisingEvents = True, .SynchronizingObject = Me } ```` because I have it in public class of my FOrm_home but OnCreate sub is in another module and I can't seem to access this watcher from there without using addhandler
@Eduards, why would you put the event handler in a module to begin with? The FileSystemWatcher belongs to the form and the event handler is modifying a control on the same form. The event handler should be in the form too. There's nothing stopping the event handler from calling a method in a module to do some processing not related to the UI. Why would the module need to access the FileSystemWatcher anyway?
Because I'm organizing my project using modules so that I can always find the sub I'm looking for. Regarding the accessing - I can't seem to reference the watcher from anoither module. However I moved the handler subs to form and now it works perfectly it seems. Just a couple more tests and I will mark your answer as correct! Best wishes to you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.