Read from port every 10 seconds

S

sight

How to make application read from port every 10 s.


Is it smart to create Thread and do it with sleep or with do-while command?
 
A

Alex Whitney

You could do it with sleep or a do-while (checking the time at each
loop). I don't really suggest it, though. I would just make a thread
that constantly reads data from the port and stores it into a
synchronized queue. Then you can read data from the queue in whatever
time intervals you want.
 
M

Mark Space

Alex said:
You could do it with sleep or a do-while (checking the time at each
loop). I don't really suggest it, though. I would just make a thread
that constantly reads data from the port and stores it into a
synchronized queue. Then you can read data from the queue in whatever
time intervals you want.

I think it kinda depends, but if there is a way to block until new data
is available, sure.

OTOH, some devices don't have a new data flag and just make data
available continuously. For example, a web cam. Using something like a
10 sec interval to slow the web cam data rate down to something
reasonable is a pretty good idea.

Since the OP is talking about using multiple threads, I assume he has
some sort of shared data/queue/synchronization scheme in mind.

To the OP: well, really I don't know, but I'm pretty sure do {} while
with a sleep() will work. I'm pretty sure that sleep doesn't guarantee
exact time intervals too. You'll get "roughly" 10 second intervals.
And sleep() can be interrupted early, which will throw an exception.

But as long as all this is ok, then it should work fine.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top