chspit
asked on
Run a process from a FileSystemWatcher only once ...how?
Hi,
I have a curious problem. I am using a WindowsService with a FileSystemWatcher component that does the following: as soon as I send a bmp image to folder A, the bmp image is converted to a gif image and saved in a folder B, from where the image is sent to a bluetooth device. The problem that I am encountering is that the process of sending the gif image is not executed only once...it keeps on running until I stop it. (i.e. the image is sent more than once). Why is that so? What can I do to make it run only once?
Below is what i've done:
__________________________ __________ _________
Private Sub FileSystemWatcher1_Changed (ByVal sender As System.Object, ByVal e As System.IO.FileSystemEventA rgs) Handles FileSystemWatcher1.Changed
Dim imgFile As System.Drawing.Image = System.Drawing.Image.FromF ile("C:\Fo lderA\imag e.bmp")
imgFile.Save("C:\FolderB\i mage.gif", System.Drawing.Imaging.Ima geFormat.G if)
Process.Start("Folder B") 'to open folder where there is the gif image
SendKeys.SendWait(" ")
SendKeys.SendWait("%fn")
SendKeys.SendWait("{RIGHT} ")
SendKeys.SendWait("{ENTER} ")
SendKeys.SendWait("%fc")
'''''(I had to use SendKeys to send the image to a bluetooth device, because other
'''''methods, such as the Environment.SpecialFolder. SendTo... , did not work)
Application.Exit() 'I tried this to exit process, but it did not work
Application.ExitThread() 'this did not work either
End Sub
__________________________ __________ __________ __________ __________ _
How can I make the above process run only once and exit?
Thanks in advance for your help.
I have a curious problem. I am using a WindowsService with a FileSystemWatcher component that does the following: as soon as I send a bmp image to folder A, the bmp image is converted to a gif image and saved in a folder B, from where the image is sent to a bluetooth device. The problem that I am encountering is that the process of sending the gif image is not executed only once...it keeps on running until I stop it. (i.e. the image is sent more than once). Why is that so? What can I do to make it run only once?
Below is what i've done:
__________________________
Private Sub FileSystemWatcher1_Changed
Dim imgFile As System.Drawing.Image = System.Drawing.Image.FromF
imgFile.Save("C:\FolderB\i
Process.Start("Folder B") 'to open folder where there is the gif image
SendKeys.SendWait(" ")
SendKeys.SendWait("%fn")
SendKeys.SendWait("{RIGHT}
SendKeys.SendWait("{ENTER}
SendKeys.SendWait("%fc")
'''''(I had to use SendKeys to send the image to a bluetooth device, because other
'''''methods, such as the Environment.SpecialFolder.
Application.Exit() 'I tried this to exit process, but it did not work
Application.ExitThread() 'this did not work either
End Sub
__________________________
How can I make the above process run only once and exit?
Thanks in advance for your help.
It is a very easy solution. The reason you are getting multiple events is because the filesystem watcher watches last access time, and everytime you touch the file it is cuasing another event to be thrown... simply put an if statement around your code to restrict event types. You only want your code to run if e.ChangeType = IO.WatcherChangeTypes.Crea ted .... but the way you have it written it will run no matter what change occurs. Do it like I have below:
Private Sub FileSystemWatcher1_Changed (ByVal sender As System.Object, ByVal e As System.IO.FileSystemEventA rgs) Handles FileSystemWatcher1.Changed
if e.ChangeType = IO.WatcherChangeTypes.Crea ted then
Dim imgFile As System.Drawing.Image = System.Drawing.Image.FromF ile("C:\Fo lderA\imag e.bmp")
imgFile.Save("C:\FolderB\i mage.gif", System.Drawing.Imaging.Ima geFormat.G if)
Process.Start("Folder B") 'to open folder where there is the gif image
SendKeys.SendWait(" ")
SendKeys.SendWait("%fn")
SendKeys.SendWait("{RIGHT} ")
SendKeys.SendWait("{ENTER} ")
SendKeys.SendWait("%fc")
end if
end sub
Private Sub FileSystemWatcher1_Changed
if e.ChangeType = IO.WatcherChangeTypes.Crea
Dim imgFile As System.Drawing.Image = System.Drawing.Image.FromF
imgFile.Save("C:\FolderB\i
Process.Start("Folder B") 'to open folder where there is the gif image
SendKeys.SendWait(" ")
SendKeys.SendWait("%fn")
SendKeys.SendWait("{RIGHT}
SendKeys.SendWait("{ENTER}
SendKeys.SendWait("%fc")
end if
end sub
ASKER
Hi KeirGordon,
if i try as you said, nothing happens...the event is not raised.
When in the beginning I create the FileSystemWatcher I specify the NotifyFilter to LastAccess. so if I then add your line, still nothing happens when a new file is created.
if i try as you said, nothing happens...the event is not raised.
When in the beginning I create the FileSystemWatcher I specify the NotifyFilter to LastAccess. so if I then add your line, still nothing happens when a new file is created.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Here is a chunk of code from my OnChange event handler....
Static LastRun As Date
System.Threading.Thread.Sl
If DateDiff(DateInterval.Seco
LastRun = Now
' do something
Else
' only once please!
Endif