Timer to sleep 1/10th of a second

M

moti

I need to check for the existence of a file (the filename and folder
are known). The file may be created at any time, before or after the
start of the file check. As I do not want to burden the CPU, I figured
I would create a timer and sleep for 1/10th of a second interval. When
it finds this text file, the program will open, read, close and delete
it. Then it will wait again for the existence of this file (forever).

Could someone please tell me what is necessary to accomplish this in
vbnet?
 
K

Ken Tucker [MVP]

Hi,

Add a timer control to your form set the interval to 100
milliseconds.

Ken
------------------
I need to check for the existence of a file (the filename and folder
are known). The file may be created at any time, before or after the
start of the file check. As I do not want to burden the CPU, I figured
I would create a timer and sleep for 1/10th of a second interval. When
it finds this text file, the program will open, read, close and delete
it. Then it will wait again for the existence of this file (forever).

Could someone please tell me what is necessary to accomplish this in
vbnet?
 
H

Herfried K. Wagner [MVP]

moti said:
I need to check for the existence of a file (the filename and folder
are known). The file may be created at any time, before or after the
start of the file check. As I do not want to burden the CPU, I figured
I would create a timer and sleep for 1/10th of a second interval. When
it finds this text file, the program will open, read, close and delete
it. Then it will wait again for the existence of this file (forever).

You can add a 'System.Windows.Forms.Timer' to you form, set the interval
accordingly and add a handler to its 'Tick' event. There you can place the
code used to check if the file exists.

Alternatively you could use something like this:

\\\
Do Ultil File.Exists(...) OrElse QuitApplication
Thread.Sleep(10)
Loop
///
 
M

Michael D. Ober

Is the Windows API FindFirstChangeNotification(), which instructs Windows to
watch a directory for certain changes implemented in the .NET Framework? If
so, this would be a better solution than a timer. Timers, by their very
nature, consume CPU resources every time they fire. This API doesn't.

Mike.
 
H

Herfried K. Wagner [MVP]

Michael D. Ober said:
Is the Windows API FindFirstChangeNotification(), which instructs Windows
to
watch a directory for certain changes implemented in the .NET Framework?

As Greg already said, using the FileSystemWatcher component is the way to
go.
 
M

moti

I thank you all for your quick response.
The FileSystemWatcher seems to be the proper way to do it, though it
does not mention if it uses a lot of cpu time while waiting for the
event to fire.

Does it?
 
G

Greg Burns

I suspect it is uses less cpu time than a timer, but I don't know for sure.
The book I have Balena's "Programming VB.NET" suggests using the
WaitForChange method for more efficient code if you are simply waiting for
changes in a specified path.

Dim fsw As New System.IO.FileSystemWatcher("c:\", "test.txt")

Dim res As System.IO.WaitForChangedResult

res = fsw.WaitForChanged(System.IO.WatcherChangeTypes.All, 10000)

If res.TimedOut Then
MsgBox("10 sec timeout")
Else
Dim ct As String
ct = [Enum].GetName(GetType(System.IO.WatcherChangeTypes),
res.ChangeType)
MsgBox("Event:" & res.Name & " (" & ct & ")")
End If

Greg
 
M

Michael D. Ober

The underlying Windows API for the FileSystemWatcher hooks the file system
for notification. Therefore, there is no CPU time used by this API. Based
on this answer to my earlier question, I looked up the FileSystemWatcher in
the MSDN library and noticed that it can be used to monitor individual or
groups of files - not just directories. This level of monitoring is more
selective than the Windows API, so the FileSystemWatcher class will use a
little bit of CPU time when monitoring individual files. If there is an
extremely huge amount of file io to the directory your file of interest is
in, you might generate more CPU cycles than using a timer. However, the
chances of that are so remote as to not warrant this concern. A bigger
problem with the underlying API is that if your file of interest is on a
non-Windows hosted share such as Samba, it simply won't work because the
target host OS doesn't support it. In this case, you have no choice but to
use a timer.

Mike Ober.
 
M

moti H

Thanks for the reply.
The problem is that this program may be run 10 to 20 times on the same
pc all running simultaneously. This might chew a lot of CPU time. That
is why I was considering letting the program sleep for 1/10 of a second,
thereby assuring that it can only be done 10 times a second, not 100 or
1000. But I am not sure if this itself will take a lot of CPU time, nor
do I know if sleeping is allowed in a main program and not using
threading.
 
G

Greg Burns

A bigger
problem with the underlying API is that if your file of interest is on a
non-Windows hosted share such as Samba, it simply won't work because the
target host OS doesn't support it.

FYI Windows 98 is not support either. AFAIK only Windows ME, NT, 2000, XP.

Greg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top