Link to home
Start Free TrialLog in
Avatar of jewee
jewee

asked on

Poll a directory every 10 seconds - HELP!

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.
Avatar of jewee
jewee

ASKER

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?
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);
    do_your_directory();
}

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

Cheers,
Stefan
Avatar of jewee

ASKER

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.

Avatar of Axter
What is your OS, and what compiler are you using?

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

ASKER

Linux, g++
Avatar of jewee

ASKER

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?
jewee,
> 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?

Stefan
Avatar of jewee

ASKER

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.

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.

ASKER CERTIFIED SOLUTION
Avatar of stefan73
stefan73
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial