Poll a directory every 10 seconds - HELP!

Posted on 2004-05-02
Last Modified: 2010-04-01
I asked a question earlier about polling a directory.  I wanted to know, what is recommended as far as looking in this directory every 10 seconds.  Should I use the sleep() function?  If I use that, I think the user will not be able to access other functions.
Question by:jewee

Author Comment

ID: 10974728
Also, as I am polling a directory every 10 seconds, I need to display some data from that file in a GUI every 20 seconds.

Any recommendations?
LVL 12

Expert Comment

ID: 10975362
Hi jewee,
The easiest way should be to have something like:

static time_t last_poll=0;

if(time(0)-last_poll >= 10){
    last_poll = time(0);

If you have a main polling loop, you can do sleep(1) there and evaluate time(0) only once.


Author Comment

ID: 10975436
How about the GUI display?  Should I consider separate threads?  I'm kind of confused as far as when to call this function to keep track of the time.

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

LVL 30

Expert Comment

ID: 10975764
What is your OS, and what compiler are you using?

For UNIX & Linux, you should use a seperate thread for this.

Author Comment

ID: 10975766
Linux, g++

Author Comment

ID: 10975791
This is how my code is set up...

I have a function to poll a directory.  Within that function, if file is found, then call the ProcessFile function.  Then ProcessFile function calls the DisplayData function which then calls the updateGUI function.

If I was to have separate threads, how would I go about implementing that?
LVL 12

Expert Comment

ID: 10975834
> If I was to have separate threads, how would I go about implementing that?

If you can update the GUI totally independent of other actions, threads are fine. They are just a little bit (or rather a Big Bit) more difficult to synchronize & test.

If threads make sense at all very much depends on the GUI update complexity. For example if you have a batch-like processing of data from an input directory and your effort to update the GUI is minimal, the thread implementation overhead just might not be worth it.

If you have a real interactive application and a complex drawing routine, threads might be fine. But you can still handle your directory checking in the app's event check routine.

Does it hurt your application when the 10/20 intervals are not exactly kept - i.e., shift by a second once in a while?


Author Comment

ID: 10975914
No, it wouldn't hurt it if it was off by a second occasionally.   I was reading some other posts and there is a way to set an alarm?  Is that similar to a timer?  Would that be something I can do?  I'm just not sure of the implementation.  I have the program working but with no alarm to continue polling the directory while files are being processed.
LVL 22

Expert Comment

ID: 10976556

Polling a directory is generally a *losing* idea.  I've tried it, several times, eventually went to some much better performing and less ambiguous communication path, such as named pipes, signals, TCP/IP, windows messages, etc.    Never looked back.

LVL 12

Accepted Solution

stefan73 earned 500 total points
ID: 10976631
> Polling a directory is generally a *losing* idea.

I agree. But sometimes you don't have a choice.

If you want to check for new entries, you can check the directory's time stamp (using stat()). It's updated when a new file is added. The effort should be acceptable.

Alarms are questionnable, as they go off when you least need them, such as in the middle of processing something else, while your system is in an inconsistent state.


Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

830 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