Hi, I am a new VB developer
I have created a file watcher but I have a problem with the file watcher
here is the code:
Code:
Inherits System.Windows.Forms.Form
Private watchfolder As IO.FileSystemWatcher
Private Sub btnstart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnstart.Click
watchfolder = New System.IO.FileSystemWatcher
'this is the path we want to monitor
watchfolder.Path = txtfolder.Text
watchfolder.NotifyFilter = IO.NotifyFilters.DirectoryName
watchfolder.NotifyFilter = watchfolder.NotifyFilter Or IO.NotifyFilters.FileName
watchfolder.NotifyFilter = watchfolder.NotifyFilter Or IO.NotifyFilters.Attributes
'Set this property to true to start watching
watchfolder.EnableRaisingEvents = True
btnstart.Enabled = False
btnstop.Enabled = True
AddHandler watchfolder.Changed, AddressOf logchange
AddHandler watchfolder.Created, AddressOf logchange
AddHandler watchfolder.Deleted, AddressOf logchange
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
txtmonitor.Text = "haha"
End Sub
Private Sub btnstop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnstop.Click
' Stop watching the folder
watchfolder.EnableRaisingEvents = False
btnstart.Enabled = True
btnstop.Enabled = False
End Sub
Public Sub logchange(ByVal source As Object, ByVal e As System.IO.FileSystemEventArgs)
'Frm = New Form1
MsgBox(txtfolder.Text)
MsgBox(txtmonitor.Text)
If e.ChangeType = IO.WatcherChangeTypes.Changed Then
txtmonitor.Text &= "File " & e.FullPath & " has been modified" & vbCrLf
End If
If e.ChangeType = IO.WatcherChangeTypes.Created Then
txtmonitor.Text &= "File " & e.FullPath & " has been created" & vbCrLf
End If
If e.ChangeType = IO.WatcherChangeTypes.Deleted Then
txtmonitor.Text &= "File " & e.FullPath & " has been deleted" & vbCrLf
End If
End Sub
And I have the Following error
InvalidOperationException
"Cross-thread operation not valid: Control 'txtmonitor' accessed from a thread other than the thread it was create on."
Please Help me..
thank you...