Solved

Poll a directory every 10 seconds - HELP!

Posted on 2004-05-02
10
198 Views
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.
0
Comment
Question by:jewee
10 Comments
 

Author Comment

by:jewee
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?
0
 
LVL 12

Expert Comment

by:stefan73
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);
    do_your_directory();
}

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

Cheers,
Stefan
0
 

Author Comment

by:jewee
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.

0
 
LVL 30

Expert Comment

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

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

Author Comment

by:jewee
ID: 10975766
Linux, g++
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:jewee
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?
0
 
LVL 12

Expert Comment

by:stefan73
ID: 10975834
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
0
 

Author Comment

by:jewee
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.
0
 
LVL 22

Expert Comment

by:grg99
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.

0
 
LVL 12

Accepted Solution

by:
stefan73 earned 500 total points
ID: 10976631
grg99,
> 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.

Stefan
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

896 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now