Reputation: 369
Imports System.IO
Public Class Form1
Dim watcher As New FileSystemWatcher()
Dim FileChanged As Boolean
Delegate Sub UpdateFileChangeDelegate()
Private Sub Button_Click(sender As System.Object, e As System.EventArgs) Handles Button.Click
watcher.Path = "C:\Demo Image Pool"
watcher.NotifyFilter = NotifyFilters.LastWrite
watcher.Filter = "DemoImage.raw"
watcher.SynchronizingObject = Me
AddHandler watcher.Changed, AddressOf OnChanged
watcher.EnableRaisingEvents = True
TextBoxMessage.Text = "Looking for file change"
'The code below relates to Application
Dim myProcess As New Process()
myProcess.StartInfo.FileName = "CaptureImage.exe"
myProcess.StartInfo.Arguments = "60 500 8 100"
myProcess.StartInfo.UseShellExecute = False
myProcess.StartInfo.CreateNoWindow = True
myProcess.StartInfo.RedirectStandardInput = True
myProcess.StartInfo.RedirectStandardOutput = True
myProcess.StartInfo.RedirectStandardError = True
myProcess.Start()
myProcess.WaitForExit(28000)
myProcess.Close()
While 1
If FileChanged = True Then
TextBoxMessage.Text = "Hello World"
Exit While
End If
End While
End Sub
Private Shared Sub OnChanged(ByVal source As Object, ByVal e As FileSystemEventArgs)
Form1.FileChanged = True
End Sub
End Class
In the above code, the CaptureImage.exe captures a new image, "DemoImage.raw", at the FileWatcher location. I am changing the Boolean variable to true in the File Change event handler, however in the main thread, I am not able to get out of the loop. The reason being, I think, variable FileChanged being not changed or the change being not reflected. I would greatly appreciate if someone can point out where I have gone wrong. The form consists of simply a button and textbox.
Thanks!
Upvotes: 0
Views: 84
Reputation: 54417
The problem is that you're using a default instance and default instances are thread-specific. As such, you're actually creating a new Form1 instance and setting its field rather than the Form1 instance that you have open. No form should refer to its own default instance anyway, so that should be Me.FileChanged
rather than Form1.FileChanged
.
Apart from that, I really hope that is just some really dirty testing code because you have a busy wait loop there and that's about the worst code you can possibly write.
Upvotes: 1