0

I have a form that checkes if there are some changes made in a folder. I do this with the FileSystemWatcher. When a change is notiyfied another form has to be loaded. The second form is loaded, but the controls are not visible. When I load the second form by clicking a button, the contols are visible. I think it's because of the If statement, but I don't know for sure. Can someone helpe me out with this?

Imports System.IO
'Imports System.Diagnostics

Public Class Settings
  Public watchfolder As FileSystemWatcher
  Private Sub Settings_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    txt_watchpath.Text = "D:\temp"
    If txt_watchpath.Text = "" Then
        btn_startwatch.Enabled = False
    End If
  End Sub

  Private Sub txt_watchpath_TextChanged(sender As Object, e As EventArgs) Handles txt_watchpath.TextChanged
    If Me.Text <> "" Then
        btn_startwatch.Enabled = True
    End If
  End Sub

  Private Sub btn_startwatch_Click(sender As Object, e As EventArgs) Handles btn_startwatch.Click
    watchfolder = New System.IO.FileSystemWatcher()

    'this is the path we want to monitor
    watchfolder.Path = txt_watchpath.Text

    'Add a list of Filter we want to specify
    'make sure you use OR for each Filter as we need to
    'all of those 

    watchfolder.NotifyFilter = IO.NotifyFilters.DirectoryName
    watchfolder.NotifyFilter = watchfolder.NotifyFilter Or
    IO.NotifyFilters.LastWrite
    watchfolder.NotifyFilter = watchfolder.NotifyFilter Or
    IO.NotifyFilters.Attributes

    ' add the handler to each event
    AddHandler watchfolder.Changed, AddressOf logchange
    AddHandler watchfolder.Created, AddressOf logchange
    'AddHandler watchfolder.Deleted, AddressOf logchange

    ' add the rename handler as the signature is different
    AddHandler watchfolder.Renamed, AddressOf logrename

    'Set this property to true to start watching
    watchfolder.EnableRaisingEvents = True
    watchfolder.IncludeSubdirectories = False

    btn_startwatch.Enabled = False
    btn_stop.Enabled = True
    Me.Hide()

    'End of code for btn_start_click
  End Sub

  Private Sub logchange(ByVal source As Object, ByVal e As System.IO.FileSystemEventArgs)

    If e.ChangeType = IO.WatcherChangeTypes.Changed Then
        FrmPopup.Fullpath = e.FullPath
        FrmPopup.Activity = e.Name & " is changed"
        FrmPopup.Show()
    End If

    If e.ChangeType = IO.WatcherChangeTypes.Created Then
        FrmPopup.Fullpath = e.FullPath
        FrmPopup.Activity = e.Name & " is made"
        test.Show()
    End If
   
  End Sub

  Private Sub logrename(ByVal source As Object, ByVal e As System.IO.RenamedEventArgs)

    FrmPopup.Fullpath = e.FullPath
    FrmPopup.Activity = e.OldName & " is changed to " & e.Name
    FrmPopup.Show()

  End Sub

  Private Sub btn_stop_Click(sender As Object, e As EventArgs) Handles btn_stop.Click
    FrmPopup.Show()
  End Sub
End Class
LarsTech
  • 80,625
  • 14
  • 153
  • 225
Bjorn M
  • 7
  • 5
  • Show us the code you use to load the form by click vs this code. I don't see a ` = New FrmPopup()` in this posted code. – LarsTech Aug 26 '20 at 14:04
  • @LarsTech, I placed the complete code into the question. In ` Private Sub btn_stop_Click(sender As Object, e As EventArgs) Handles btn_stop.Click` is the button click statement to show the popup form. The popup form is just a form with two labels and a button. When I click the button. The controls are visible in the popup form. – Bjorn M Aug 26 '20 at 14:30
  • What is `test.Show()` doing? – LarsTech Aug 26 '20 at 14:39
  • The same as FrmPopup.Show(). It is another form with a button. Just to see if it's not a problem with the FrmPopup. – Bjorn M Aug 26 '20 at 14:42

1 Answers1

0

FileSystemWatcher events are not generated by UI thread. To be inside your UI thread you need to use something like this:

Private Sub logchange(ByVal source As Object, ByVal e As System.IO.FileSystemEventArgs)

    If InvokeRequired Then
        Me.Invoke(New MethodInvoker(Sub()

                                        If e.ChangeType = IO.WatcherChangeTypes.Changed Then
                                            FrmPopup.Fullpath = e.FullPath
                                            FrmPopup.Activity = e.Name & " is changed"
                                            FrmPopup.Show()
                                        End If

                                        If e.ChangeType = IO.WatcherChangeTypes.Created Then
                                            FrmPopup.Fullpath = e.FullPath
                                            FrmPopup.Activity = e.Name & " is made"
                                            test.Show()
                                        End If

                                    End Sub))
    End If

End Sub

Private Sub logrename(ByVal source As Object, ByVal e As System.IO.RenamedEventArgs)

    If InvokeRequired Then
        Me.Invoke(New MethodInvoker(Sub()

                                        FrmPopup.Fullpath = e.FullPath
                                        FrmPopup.Activity = e.OldName & " is changed to " & e.Name
                                        FrmPopup.Show()

                                    End Sub))
    End If

End Sub
G3nt_M3caj
  • 2,497
  • 1
  • 14
  • 16
  • 1
    [get filesystemwatcher events to occur on main UI thread](https://stackoverflow.com/a/22491246/719186) – LarsTech Aug 26 '20 at 17:01
  • Avoid that as FileSystemWatcher is made to check on files not inside of the UI thread. Doesn’t make sense otherwise the use of. – G3nt_M3caj Aug 26 '20 at 17:06
  • Re *Avoid that* - might need to tell Microsoft then, '[cos it's what they do..](https://i.stack.imgur.com/cFv8A.png) – Caius Jard Aug 26 '20 at 18:36
  • The default value of SynchronizingObject (we are talking about) is NULL. There is a valid reason this is a parameter, is optional and Events generated by FileSystemWatcher are by System not by your app. The scenarios of use to FileSystemWatcher doesn’t request a Form/Control/UI to be active in the moment FileSystemWatcher are listening. So I repeat. Avoid use of SynchronizingObject. Microsoft have a lot of methods could be avoided or better to say to use only in specific cyrcomstances. – G3nt_M3caj Aug 26 '20 at 19:29