Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Run a process from a FileSystemWatcher only once     ...how?

Posted on 2004-04-23
4
Medium Priority
?
710 Views
Last Modified: 2012-05-04
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.FileSystemEventArgs) Handles FileSystemWatcher1.Changed

        Dim imgFile As System.Drawing.Image = System.Drawing.Image.FromFile("C:\FolderA\image.bmp")
        imgFile.Save("C:\FolderB\image.gif", System.Drawing.Imaging.ImageFormat.Gif)

        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.
0
Comment
Question by:chspit
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 41

Expert Comment

by:graye
ID: 10904784
Yeah, I've had the same problems.... My solution (which is anything but elegant!) is to create a timestamp and check for repeat calls...

Here is a chunk of code from my OnChange event handler....

        Static LastRun As Date

            System.Threading.Thread.Sleep(1000)
            If DateDiff(DateInterval.Second, LastRun, Now) > 1 Then
                LastRun = Now
                ' do something
            Else
                ' only once please!
            Endif
0
 
LVL 5

Expert Comment

by:KeirGordon
ID: 10908221
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.Created .... 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.FileSystemEventArgs) Handles FileSystemWatcher1.Changed

if e.ChangeType = IO.WatcherChangeTypes.Created then

     Dim imgFile As System.Drawing.Image = System.Drawing.Image.FromFile("C:\FolderA\image.bmp")
        imgFile.Save("C:\FolderB\image.gif", System.Drawing.Imaging.ImageFormat.Gif)

        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
0
 

Author Comment

by:chspit
ID: 10916920
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.
0
 
LVL 5

Accepted Solution

by:
tgannetts earned 1500 total points
ID: 10917401
The problem with the above is that you are trying to catch Created events in a Changed Event Handler. To catch the created events, use the Handles FileSystemWatcher1.Created handler instead. There should only be one Created event raised for each file deposited.

The problem comes if you are overwriting files. From your code it looks like the files are always called image.bmp. This does not generate a Created event, but instead a Changed event. You would therefore have to delete the file after you have generated the gif file to make sure each file dropped into the folder would generate a Created event in order to use this method.

Tom.
0

Featured Post

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question