[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Poll a directory every 10 seconds - HELP!

Posted on 2004-05-02
10
Medium Priority
?
205 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 

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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

656 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