how to leave a loop after 2 sec.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rainer

    how to leave a loop after 2 sec.

    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.BytesToRe ad < 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


  • =?Utf-8?B?S0g=?=

    #2
    RE: how to leave a loop after 2 sec.


    DateTime quitTime = DateTime.Now.Ad dSeconds(2.0);

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



    "Rainer" wrote:
    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.BytesToRe ad < 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
    >
    >
    >

    Comment

    • Rainer

      #3
      Re: how to leave a loop after 2 sec.


      "Peter Duniho" <NpOeStPeAdM@nn owslpianmk.coms chrieb im Newsbeitrag
      news:op.t6k8lbw l8jd0ej@petes-computer.local. ..
      On Fri, 15 Feb 2008 08:55:01 -0800, KH <KH@discussions .microsoft.com>
      wrote:
      >
      DateTime quitTime = DateTime.Now.Ad dSeconds(2.0);
      >
      while (port.BytesToRe ad < 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


      Comment

      • verbiest@gmail.com

        #4
        Re: how to leave a loop after 2 sec.

        DateTime quitTime = DateTime.Now.Ad dSeconds(2.0);
        >
        while (port.BytesToRe ad < 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

        Comment

        • Rainer

          #5
          Re: how to leave a loop after 2 sec.

          <verbiest@gmail .comschrieb im Newsbeitrag
          news:85b8fe26-4f48-49df-8478-b183c2ffff6a@41 g2000hsc.google groups.com...
          >DateTime quitTime = DateTime.Now.Ad dSeconds(2.0);
          >>
          >while (port.BytesToRe ad < 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/


          Hi Kristof,

          thanks, i will check this out tomorrow.

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


          Comment

          Working...