0

I want to work program Watching Folder Activity But I am having a problem in the course of the work program Shows me an error message Look this picture [ https://i.stack.imgur.com/ApV7k.png ] This Full source my program :

Imports System.IO
Imports System.Diagnostics
Public Class Form1
    Public watchfolder As FileSystemWatcher
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub

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

        'this is the path we want to monitor
        watchfolder.Path = TextBox1.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.FileName
        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

        Button1.Enabled = False
        Button2.Enabled = True

        'End of code for btn_start_click
    End Sub
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        watchfolder.EnableRaisingEvents = False
        Button1.Enabled = True
        Button2.Enabled = False
    End Sub
    Public Sub logrename(ByVal source As Object, ByVal e As  _
                            System.IO.RenamedEventArgs)
        TextBox2.Text &= "File" & e.OldName & _
                      " has been renamed to " & e.Name & vbCrLf
    End Sub
    Private Sub logchange(ByVal source As Object, ByVal e As  _
                        System.IO.FileSystemEventArgs)
        If e.ChangeType = IO.WatcherChangeTypes.Changed Then
            TextBox2.Text &= "File " & e.FullPath & _
                                    " has been modified" & vbCrLf
        End If
        If e.ChangeType = IO.WatcherChangeTypes.Created Then
            TextBox2.Text &= "File " & e.FullPath & _
                                     " has been created" & vbCrLf
        End If
        If e.ChangeType = IO.WatcherChangeTypes.Deleted Then
            TextBox2.Text &= "File " & e.FullPath & _
                                    " has been deleted" & vbCrLf
        End If
    End Sub
End Class
tshepang
  • 12,111
  • 21
  • 91
  • 136

3 Answers3

0

The events will be called on a separate thread automatically. You can't change the value of a control created on another thread. So when you are trying to update the value of TextBox2 created on the main thread, an error appears. You can use a delegate function and check the control's InvoqueRequired property. Maybe this could help you : Automating the InvokeRequired code pattern

Community
  • 1
  • 1
HLG13
  • 63
  • 5
0

The problem is that the FileSystemWatcher which is running in a separate thread tries to access a WinForms control. However only the main thread of your application can modify the UI elements. This question should solve the problem: WinForms interthread modification

Community
  • 1
  • 1
alu
  • 759
  • 7
  • 20
0

This delegates function may solve your problem.

Imports System.IO
Imports System.Diagnostics
Public Class Form1
    Public watchfolder As FileSystemWatcher
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub

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

        'this is the path we want to monitor
        watchfolder.Path = TextBox1.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.FileName
        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

        Button1.Enabled = False
        Button2.Enabled = True

        'End of code for btn_start_click
    End Sub
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        watchfolder.EnableRaisingEvents = False
        Button1.Enabled = True
        Button2.Enabled = False
    End Sub
    Public Sub logrename(ByVal source As Object, ByVal e As  _
                            System.IO.RenamedEventArgs)
        Invoke(New SetTextDel(AddressOf SetText), "File" & e.OldName & _
                      " has been renamed to " & e.Name & vbCrLf)
    End Sub
    Private Sub logchange(ByVal source As Object, ByVal e As  _
                        System.IO.FileSystemEventArgs)
        If e.ChangeType = IO.WatcherChangeTypes.Changed Then
            Invoke(New SetTextDel(AddressOf SetText), "File " & e.FullPath & _
                                    " has been modified" & vbCrLf)
        End If
        If e.ChangeType = IO.WatcherChangeTypes.Created Then
            Invoke(New SetTextDel(AddressOf SetText), "File " & e.FullPath & _
                                     " has been created" & vbCrLf)
        End If
        If e.ChangeType = IO.WatcherChangeTypes.Deleted Then
            Invoke(New SetTextDel(AddressOf SetText), "File " & e.FullPath & _
                                    " has been deleted" & vbCrLf)
        End If
    End Sub

    Private Delegate Sub SetTextDel(ByVal msg As String)

    Private Sub SetText(ByVal msg As String)
        TextBox2.Text = msg
    End Sub

End Class
Tuan Zaidi
  • 343
  • 1
  • 14