Solved

Poll a directory every 10 seconds - HELP!

Posted on 2004-05-02
10
197 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

747 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

13 Experts available now in Live!

Get 1:1 Help Now