Connecting Tech Pros Worldwide Forums | Help | Site Map

How to constantly check something without using a Timer

Newbie
 
Join Date: Nov 2008
Posts: 9
#1: Nov 18 '08
Hi everyone, I'm an old school vb6 user trying to transition to VB.NET.

Can anyone suggest a better approach to constantly checking something without using a timer?

I'm trying to constantly check a folder of text files to get the date modified of each file. I have no problems creating the filesystem object to extract the file info and dates. Currently using a timer I know is a poor approach to this but need some insight into a better way of constantly looping so the system resources aren't hogged.

Any help would be greatly appreciated.

joedeene's Avatar
Site Addict
 
Join Date: Jul 2008
Location: US of A
Posts: 587
#2: Nov 18 '08

re: How to constantly check something without using a Timer


I would use a timer? If you want to constantly refresh a directory's files and information for each file I'd use a timer but set its refresh rate/interval to like 1000 milliseconds. I know it would be better then using a Do While Loop statement, because it would freeze the program up.

joedeene
lotus18's Avatar
Site Addict
 
Join Date: Nov 2007
Location: Zamboanga City, Philippines
Posts: 858
#3: Nov 18 '08

re: How to constantly check something without using a Timer


Try FileSystemWatcher component : )


Rey Sean
balabaster's Avatar
Moderator
 
Join Date: Mar 2007
Location: Canada
Posts: 757
#4: Nov 18 '08

re: How to constantly check something without using a Timer


Quote:

Originally Posted by lotus18

Try FileSystemWatcher component

Definitely the best approach.

There are some caveats to watch out for though - when you do a file rename or a save it'll cause a bunch of extraneous events to occur, so what you really have to do is wait for the event and then attempt to get a file lock on that file. Once the file lock occurs, the file has finished saving/writing and then you can go on to trigger what you need to.

If you go off the regular event, then you can run into concurrency issues.

The changed event can run many times on a file save. The created event however, is only raised once.
Newbie
 
Join Date: Nov 2008
Posts: 5
#5: Nov 18 '08

re: How to constantly check something without using a Timer


It's also worth noting that the FileSystemWatcher implementation does not work well for network directories (access through UNC paths or as mapped drive).
Newbie
 
Join Date: Nov 2008
Posts: 9
#6: Nov 18 '08

re: How to constantly check something without using a Timer


Everyone, Thank you very much. I truly appreciate your replies. Your recommendations worked out for me!
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,777
#7: Nov 19 '08

re: How to constantly check something without using a Timer


Quote:

Originally Posted by boyank

It's also worth noting that the FileSystemWatcher implementation does not work well for network directories (access through UNC paths or as mapped drive).

I'm curious about this comment. You say it doesn't work well over a mapped drive or UNC path. I assume you have had bad experiences with this?

The reason I ask is because a couple of my programs use this extensively and I haven't seen any problems in the last year.

9 computers; each dumping 1800 jpg's per 8-hour day into directories on a server.

13 other computers reacting to those new files based on FileSystemWatcher, to either display or process the files in a given way.

I haven't seen an issue so far. What kind of issues have you run in to so I can try to head off problems?

Regards,
tlhIn'toQ
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,777
#8: Nov 19 '08

re: How to constantly check something without using a Timer


Quote:

Originally Posted by balabaster

Definitely the best approach.

There are some caveats to watch out for though - when you do a file rename or a save it'll cause a bunch of extraneous events to occur, so what you really have to do is wait for the event and then attempt to get a file lock on that file. Once the file lock occurs, the file has finished saving/writing and then you can go on to trigger what you need to.

If you go off the regular event, then you can run into concurrency issues.

The changed event can run many times on a file save. The created event however, is only raised once.


What do you think of this approach? Maybe you have a better scheme that what I do now.

I usually react to the event of a new file by adding it to a list of files to be processed. Then take it off the list after processing is successful. That way I can deal with a list of 2000 path strings, instead of 2000 triggered events.
balabaster's Avatar
Moderator
 
Join Date: Mar 2007
Location: Canada
Posts: 757
#9: Nov 19 '08

re: How to constantly check something without using a Timer


Quote:

Originally Posted by tlhintoq

What do you think of this approach? Maybe you have a better scheme that what I do now.

I usually react to the event of a new file by adding it to a list of files to be processed. Then take it off the list after processing is successful. That way I can deal with a list of 2000 path strings, instead of 2000 triggered events.

Sounds good. I usually try and queue up the file into a dictionary so I don't get duplicates. I then check for an exclusive lock when I come to process the file to check that its finished processing.
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,777
#10: Nov 23 '08

re: How to constantly check something without using a Timer


A dictionary... <light bulb>... I hadn't thought of that. Just never made the connection. I always just used a List<string> and did a manual search of the list before adding to avoid duplicates. I never put 1 and 1 together to get 10, that a dictionary inherently doesn't allow duplicates. Thanks!
Reply