Loop Until File Exists

hercemer

Newcomer
Joined
Apr 21, 2003
Messages
11
I am writing a program to interface with a Motion Cam. I want the form to just sit there and wait until a file exists in the webcam directory (lookout.jpg). I also want to be able to interact with the form while waiting for the file (to change options etc).

This is the best I have come up with:
----------------------------------------------------------------------
Sub CheckForFile()
Application.DoEvents()
If System.IO.File.Exists(strLookoutPath) Then
PictureIsPresent()
End If
System.Threading.Thread.Sleep(500)
CheckForFile()
End Sub
-----------------------------------------------------------------------

This works, but-
1) I would like to get rid of the 500ms delay
2) After a few hours, the program exits (did I read somewhere that a Loop can only run a limited number of times?)

Thanks in advance...

p.s. This forum is incredible!!!!
 
Last edited:
You have a recursive method which never stops! There is no need for recursion here.

Visual Basic:
Sub CheckForFile()
    Do Until System.IO.File.Exists(strLookoutPath)
        Application.DoEvents()
    Loop
    PictureIsPresent()
End Sub
 
Look into the System.IO.FileSystemWatcher class that is a built-in class within the .NET Framework.

It provides properties that let you set which path to monitor and specify whether you ar interested in changes at the file or subdirectory level.

You can specify the types of changes you're interested in monitorying: new file creations, changes to file attributes, or changes to file size.

You might consider writing the class as a windows service. Running in the background and implementing your program when the .jpg is created or changed.

Jon
 
Squirm - that worked perfectly!!! Thanks :)

jfackler - Will look into that, looks mike maybe a better solution (possibly less resource intensive...)


Thanks ALOT for your help!!!!
 
Back
Top