how to leave a loop after 2 sec.

R

Rainer

Hello,

i want to leave a loop after 2 sec, befor it will a endless loop

the loop will delay the programm, till a port buffer is filled
with 22 bytes from device.
but not in every case the device is send 22 byte, from time to
time it send 15 or 13 bytes.


a snipped


while (port.BytesToRead < 22)
{
// do something

// if after 2 sec nomber of bytes < 22 --- > break
ore
// break after 2 seconds
}


how can i make this break , may be with a timer ?

many thanks for competent help.


Rainer
 
K

KH

DateTime quitTime = DateTime.Now.AddSeconds(2.0);

while (port.BytesToRead < 22 && DateTime.Now < quitTime)
{
...
}
 
C

christery

Hello,

i want to leave a loop after 2 sec, befor it will a endless loop

the loop will delay the programm, till a port buffer is filled
with 22 bytes from device.
but not in every case the device is send 22 byte, from time to
time it send 15 or 13 bytes.

a  snipped

while (port.BytesToRead < 22)
     {
     // do something

      // if after 2 sec nomber of bytes < 22 --- > break
      ore
      // break after 2 seconds
     }

how can i make this break , may be with a timer ?

many thanks for competent help.

Rainer

I think you should break when the message is received, or... as I do
discard that and pick then next that is OK, if the formatting allow
that of course...circular buffer at 1000 and keeping track of where it
starts/ends is all let the comm feed it with data. this is not done in
c# but vb6 but the app would look similar if I design it... think OSI
model of a network... a level = a problem...
//CY
 
P

Peter Duniho

DateTime quitTime = DateTime.Now.AddSeconds(2.0);

while (port.BytesToRead < 22 && DateTime.Now < quitTime)
{
...
}

This is not a bad solution at all. However, I'll point out that
DateTime.Now takes a non-trivial amount of time to execute. In this
situation it may not matter -- the contents of the loop may consume far
more time than the retrieval of the DateTime.Now value -- but it's
something to be aware of.

An alternative solution would be to use a timer (any of the three .NET
timer classes would be fine) and set a volatile boolean when the timer
goes off. Then in the loop, just check the value of that boolean rather
than constantly polling the time.

Finally, IMHO the best solution would be to be able able to tell from the
data read whether it's done or not. This might not be possible in this
scenario, but if you can tell the difference between a 22, 15, and 13 byte
transmission and know that you're done reading the specific length you
should expect, that is far better than using a timer to decide that you're
done reading. I think timers are better for implementing timeouts (and by
implication, error conditions) than for dealing with normal i/o behavior..

Pete
 
C

christery

I think you should break when the message is received, or... as I do
discard that and pick then next that is OK, if the formatting allow
that of course...circular buffer at 1000 and keeping track of where it
starts/ends is all let the comm feed it with data. this is not done in
c# but vb6 but the app would look similar if I design it... think OSI
model of a network... a level = a problem...
//CY- Dölj citerad text -

- Visa citerad text -

Hmm, think I lost a part of the think about the design, I dont lock
the program for the comm, its event driven and puts in any chars in
the ringbuffer.

プログラムã™ã‚‹ã“ã¨ã¯å›°é›£ã§ã‚ã‚‹

//CY
 
R

Rainer

DateTime quitTime = DateTime.Now.AddSeconds(2.0);

while (port.BytesToRead < 22 && DateTime.Now < quitTime)
{
...
}

This is not a bad solution at all. However, I'll point out that
DateTime.Now takes a non-trivial amount of time to execute. In this
situation it may not matter -- the contents of the loop may consume far
more time than the retrieval of the DateTime.Now value -- but it's
something to be aware of.



wow, the world can by easy, thank you

Rainer
 
R

Rainer

Hmm, think I lost a part of the think about the design, I dont lock
the program for the comm, its event driven and puts in any chars in
the ringbuffer.


CY,

thank you
 
V

verbiest

DateTime quitTime = DateTime.Now.AddSeconds(2.0);
while (port.BytesToRead < 22 && DateTime.Now < quitTime)

I would prefer using DateTime.UtcNow, that will be faster. Also, if
your code happens to run 1 second before the daylight-savings-time
kicks in the original code will get you into trouble. Using UtcNow
will not.

Kristof Verbiest
http://kristofverbiest.blogspot.com/
 
R

Rainer

I would prefer using DateTime.UtcNow, that will be faster. Also, if
your code happens to run 1 second before the daylight-savings-time
kicks in the original code will get you into trouble. Using UtcNow
will not.

Kristof Verbiest
http://kristofverbiest.blogspot.com/



Hi Kristof,

thanks, i will check this out tomorrow.

Grotje or Au revoir how do you like it ;-)
 

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

Top