How to write a time delay function?

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

    How to write a time delay function?

    Hello,
    I want to write a time delay function like "Timedelay(floa t time_lenth){}".
    When execute it, it will delay some seconds as long as "time_lenth "
    indicating. Could you help on how to write such a function?

    ----------------
    lovely newsgroup
    ----------------


  • markpapadakis

    #2
    Re: How to write a time delay function?


    Chen Shusheng wrote:
    Hello,
    I want to write a time delay function like "Timedelay(floa t time_lenth){}".
    When execute it, it will delay some seconds as long as "time_lenth "
    indicating. Could you help on how to write such a function?
    >
    ----------------
    lovely newsgroup
    ----------------
    usleep() is one way to do it ( whereas it is supported ).

    Comment

    • Richard Heathfield

      #3
      Re: How to write a time delay function?

      Chen Shusheng said:
      Hello,
      I want to write a time delay function like "Timedelay(floa t
      time_lenth){}".
      When execute it, it will delay some seconds as long as "time_lenth "
      indicating. Could you help on how to write such a function?
      #include <time.h>

      void Timedelay(float time_lenth)
      {
      time_t start = time(NULL);
      time_t end;
      do
      {
      end = time(NULL);
      } while(difftime( end, start) < time_lenth);
      }

      (Presumably 'lenth' is meaningful somewhere in the world.)

      --
      Richard Heathfield
      "Usenet is a strange place" - dmr 29/7/1999

      email: rjh at above domain (but drop the www, obviously)

      Comment

      • Chen Shusheng

        #4
        Re: How to write a time delay function?

        Thanks.
        "Richard Heathfield" <invalid@invali d.invalid>
        ??????:XYudnUqT lcl3tpzYRVny2A@ bt.com...
        Chen Shusheng said:
        >
        >Hello,
        > I want to write a time delay function like "Timedelay(floa t
        > time_lenth){}".
        >When execute it, it will delay some seconds as long as "time_lenth "
        >indicating. Could you help on how to write such a function?
        >
        #include <time.h>
        >
        void Timedelay(float time_lenth)
        {
        time_t start = time(NULL);
        time_t end;
        do
        {
        end = time(NULL);
        } while(difftime( end, start) < time_lenth);
        }
        >
        (Presumably 'lenth' is meaningful somewhere in the world.)
        >
        --
        Richard Heathfield
        "Usenet is a strange place" - dmr 29/7/1999

        email: rjh at above domain (but drop the www, obviously)

        Comment

        • Keith Thompson

          #5
          Re: How to write a time delay function?

          Richard Heathfield <invalid@invali d.invalidwrites :
          Chen Shusheng said:
          > I want to write a time delay function like "Timedelay(floa t
          > time_lenth){}".
          >When execute it, it will delay some seconds as long as "time_lenth "
          >indicating. Could you help on how to write such a function?
          >
          #include <time.h>
          >
          void Timedelay(float time_lenth)
          {
          time_t start = time(NULL);
          time_t end;
          do
          {
          end = time(NULL);
          } while(difftime( end, start) < time_lenth);
          }
          >
          (Presumably 'lenth' is meaningful somewhere in the world.)
          That's a portable way to do it, but not a good way to do it. By
          calling time() repeatedly in a loop, it's likely (on a
          multi-processing system) to gobble lots of CPU time.

          Most systems provide a way to do this kind of thing, though standard C
          doesn't.

          --
          Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
          San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
          We must do something. This is something. Therefore, we must do this.

          Comment

          • Richard Bos

            #6
            Re: How to write a time delay function?

            "Chen Shusheng" <csshine@gmail. comwrote:
            I want to write a time delay function like "Timedelay(floa t time_lenth){}".
            When execute it, it will delay some seconds as long as "time_lenth "
            indicating. Could you help on how to write such a function?
            You use a system-specific function, which you can ask for in a
            system-specific newsgroup.
            Alternatively, you _could_ use a busy-loop, and be killed by your
            systems manager for tying up the system.

            Richard

            Comment

            • Chen Shusheng

              #7
              Re: How to write a time delay function?


              "Richard Bos" <rlb@hoekstra-uitgeverij.nlд ÈëÏûÏ¢ÐÂÎÅ:4501 334e.1465244452 @news.xs4all.nl ...
              "Chen Shusheng" <csshine@gmail. comwrote:
              >
              > I want to write a time delay function like "Timedelay(floa t
              >time_lenth){}" .
              >When execute it, it will delay some seconds as long as "time_lenth "
              >indicating. Could you help on how to write such a function?
              >
              You use a system-specific function, which you can ask for in a
              system-specific newsgroup.
              Alternatively, you _could_ use a busy-loop, and be killed by your
              systems manager for tying up the system.
              >
              Richard
              My system is windowsXP. Where should I post to get the answer? Could you pls
              indicate?


              Comment

              • Richard Heathfield

                #8
                Re: How to write a time delay function?

                Keith Thompson said:

                <snip>
                That's a portable way to do it, but not a good way to do it.
                He didn't ask for a good way to do it. By posting in comp.lang.c, he asked
                for a comp.lang.c way to do it.

                --
                Richard Heathfield
                "Usenet is a strange place" - dmr 29/7/1999

                email: rjh at above domain (but drop the www, obviously)

                Comment

                • Nils O. Selåsdal

                  #9
                  Re: How to write a time delay function?

                  Chen Shusheng wrote:
                  "Richard Bos" <rlb@hoekstra-uitgeverij.nlд ÈëÏûÏ¢ÐÂÎÅ:4501 334e.1465244452 @news.xs4all.nl ...
                  >"Chen Shusheng" <csshine@gmail. comwrote:
                  >>
                  >> I want to write a time delay function like "Timedelay(floa t
                  >>time_lenth){} ".
                  >>When execute it, it will delay some seconds as long as "time_lenth "
                  >>indicating. Could you help on how to write such a function?
                  >You use a system-specific function, which you can ask for in a
                  >system-specific newsgroup.
                  >Alternativel y, you _could_ use a busy-loop, and be killed by your
                  >systems manager for tying up the system.
                  >>
                  >Richard
                  >
                  My system is windowsXP. Where should I post to get the answer? Could you pls
                  indicate?
                  Start at http://msdn.microsoft.com/ . If you need a newsgroup - query
                  your news server for windows programming groups.

                  Comment

                  • John F

                    #10
                    Re: How to write a time delay function?

                    "Nils O. Selåsdal" wrote:
                    Chen Shusheng wrote:
                    >"Richard Bos" <rlb@hoekstra-uitgeverij.nl>
                    >дÈëÏûÏ¢ÐÂÎÅ:4 501334e.1465244 452@news.xs4all .nl...
                    >>"Chen Shusheng" <csshine@gmail. comwrote:
                    >>>
                    >>> I want to write a time delay function like "Timedelay(floa t
                    >>>time_lenth){ }".
                    >>>When execute it, it will delay some seconds as long as
                    >>>"time_lent h"
                    >>>indicating . Could you help on how to write such a function?
                    >>You use a system-specific function, which you can ask for in a
                    >>system-specific newsgroup.
                    >>Alternatively , you _could_ use a busy-loop, and be killed by your
                    >>systems manager for tying up the system.
                    >>>
                    >>Richard
                    >>
                    >My system is windowsXP. Where should I post to get the answer?
                    >Could
                    >you pls indicate?
                    >
                    Start at http://msdn.microsoft.com/ . If you need a newsgroup -
                    query
                    your news server for windows programming groups.
                    The things to look for are SetTimer and KillTimer... These are
                    Windows-API and thus off topic here. Just to provide a reference for
                    future lookups. If you need higher resolution use the
                    multimedia-timer. It's also described there.

                    HTH
                    --
                    Johannes
                    You can have it:
                    Quick, Accurate, Inexpensive.
                    Pick two.


                    Comment

                    • Keith Thompson

                      #11
                      Re: How to write a time delay function?

                      Richard Heathfield <invalid@invali d.invalidwrites :
                      Keith Thompson said:
                      >
                      <snip>
                      >
                      >That's a portable way to do it, but not a good way to do it.
                      >
                      He didn't ask for a good way to do it. By posting in comp.lang.c, he asked
                      for a comp.lang.c way to do it.
                      No, he asked for a way to do it. If he were knowledgeable enough to
                      know in detail what's a comp.lang.c question and what isn't, he
                      wouldn't have had to ask in the first place.

                      People ask off-topic questions here all the time. The usual response
                      is to tell the OP that the question is off-topic and suggest a better
                      place to ask.

                      To someone who doesn't know C very well, it's not unreasonable to
                      think that the language might provide a good time delay function.

                      I can understand giving deliberately bad answers to "do my homework"
                      questions, but the OP was just looking for information.

                      In fact, the OP's question was very close to FAQ 19.37, "How can I
                      implement a delay, or time a user's response, with sub-second
                      resolution?".

                      --
                      Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                      San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
                      We must do something. This is something. Therefore, we must do this.

                      Comment

                      • Keith Thompson

                        #12
                        Re: How to write a time delay function?

                        "Chen Shusheng" <csshine@gmail. comwrites:
                        I want to write a time delay function like "Timedelay(floa t time_lenth){}".
                        When execute it, it will delay some seconds as long as "time_lenth "
                        indicating. Could you help on how to write such a function?
                        The comp.lang.c FAQ is at <http://www.c-faq.com/>. See question
                        19.37, "How can I implement a delay, or time a user's response, with
                        sub-second resolution?". (Quick summary: There's no good portable way
                        to do this, but there are a number of system-specific solutions.)

                        --
                        Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                        San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
                        We must do something. This is something. Therefore, we must do this.

                        Comment

                        • Richard Heathfield

                          #13
                          Re: How to write a time delay function?

                          Keith Thompson said:
                          Richard Heathfield <invalid@invali d.invalidwrites :
                          >Keith Thompson said:
                          >>
                          ><snip>
                          >>
                          >>That's a portable way to do it, but not a good way to do it.
                          >>
                          >He didn't ask for a good way to do it. By posting in comp.lang.c, he
                          >asked for a comp.lang.c way to do it.
                          >
                          No, he asked for a way to do it.
                          He got one. :-)
                          If he were knowledgeable enough to
                          know in detail what's a comp.lang.c question and what isn't, he
                          wouldn't have had to ask in the first place.
                          Not necessarily true. Plenty of people know what is topical in comp.lang.c
                          and ask topical questions.
                          People ask off-topic questions here all the time.
                          The question was not off-topic.

                          <snip>
                          In fact, the OP's question was very close to FAQ 19.37, "How can I
                          implement a delay, or time a user's response, with sub-second
                          resolution?".
                          True enough - although, as you clearly spotted, he actually asked for a
                          resolution in seconds, rather than anything finer.

                          --
                          Richard Heathfield
                          "Usenet is a strange place" - dmr 29/7/1999

                          email: rjh at above domain (but drop the www, obviously)

                          Comment

                          • Mark McIntyre

                            #14
                            Re: How to write a time delay function?

                            On Fri, 8 Sep 2006 17:29:00 +0800, in comp.lang.c , "Chen Shusheng"
                            <csshine@gmail. comwrote:
                            >
                            >My system is windowsXP. Where should I post to get the answer? Could you pls
                            >indicate?
                            The very first thing you should do is search the MSDN or your compiler
                            documentation. Typically such functions tell are used to make your
                            programme "sleep". Thats a hint for the search.
                            --
                            Mark McIntyre

                            "Debugging is twice as hard as writing the code in the first place.
                            Therefore, if you write the code as cleverly as possible, you are,
                            by definition, not smart enough to debug it."
                            --Brian Kernighan

                            Comment

                            • Mark McIntyre

                              #15
                              Re: How to write a time delay function?

                              On Fri, 8 Sep 2006 15:57:18 +0200, in comp.lang.c , "John F"
                              <spam@127.0.0.1 wrote:
                              >
                              >The things to look for are SetTimer and KillTimer...
                              Euh, no. but thats why we don't ask or answer system-specific
                              questions here.
                              --
                              Mark McIntyre

                              "Debugging is twice as hard as writing the code in the first place.
                              Therefore, if you write the code as cleverly as possible, you are,
                              by definition, not smart enough to debug it."
                              --Brian Kernighan

                              Comment

                              Working...