Imports System.IO

Public Class Watcher

    Private WithEvents Watch As New FileSystemWatcher()

    Private Sub cmdStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStart.Click
        Watch.Path = txtDir.Text
        Watch.Filter = txtTypes.Text
        Watch.EnableRaisingEvents = True
    End Sub

    Private Sub cmdStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStop.Click
        Watch.EnableRaisingEvents = False
    End Sub


    Private Sub Watch_Created(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles Watch.Created
        ' Pass call to the correct thread - przekaż wywołanie do odpowiedniego wątku.
        lstNewFiles.Invoke(New UpdateListDelegate(AddressOf UpdateList), e.Name)
    End Sub

    Private Delegate Sub UpdateListDelegate(ByVal name As String)
    Private Sub UpdateList(ByVal name As String)
        lstNewFiles.Items.Add("Nowy plik: " & name)
    End Sub

End Class
