wait a few seconds

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

    wait a few seconds

    Hello,

    I want the program to wait a few seconds between executing code. It
    should look something like this:


    public sub xx()
    ...code...
    wait(2) 'wait 2 seconds
    ...code...
    end sub


    public sub wait(byval secs as integer)
    ...???...
    end sub



    Does anyone know how to program this 'waiting'? I've looked for the
    timer control, but I don't find the right methods for this.

    Thanks,
    Snuyt

  • Cor

    #2
    Re: wait a few seconds

    Hallo Snuyt

    threading.threa d.sleep(2000)

    Cor
    [color=blue]
    > I want the program to wait a few seconds between executing code. It
    > should look something like this:
    >
    >
    > public sub xx()
    > ...code...
    > wait(2) 'wait 2 seconds
    > ...code...
    > end sub
    >
    >
    > public sub wait(byval secs as integer)
    > ...???...
    > end sub
    >
    >
    >
    > Does anyone know how to program this 'waiting'? I've looked for the
    > timer control, but I don't find the right methods for this.
    >
    > Thanks,
    > Snuyt
    >[/color]


    Comment

    • Tom Leylan

      #3
      Re: wait a few seconds

      I believe it would help if we got just a bit more information.

      What is the purpose of the "wait"? The reason it matters is perhaps (and I
      don't mean anything by it) waiting isn't the thing to do. You want to tie
      up your application so that it is unable to do anything for 2 seconds? You
      can put it into a tight loop and watch the clock but nothing (probably) is
      going to happen while it spins around the loop.

      Are you waiting for a file to print or a computation to finish? A delay
      between each attempt to login? You would tend to wait a different way on
      these things.

      Tom


      "Snuyt" <snuyt_nospam@s kynet.be> wrote in message
      news:4023e4f6$0 $319$ba620e4c@n ews.skynet.be.. .[color=blue]
      > Hello,
      >
      > I want the program to wait a few seconds between executing code. It
      > should look something like this:
      >
      >
      > public sub xx()
      > ...code...
      > wait(2) 'wait 2 seconds
      > ...code...
      > end sub
      >
      >
      > public sub wait(byval secs as integer)
      > ...???...
      > end sub
      >
      >
      >
      > Does anyone know how to program this 'waiting'? I've looked for the
      > timer control, but I don't find the right methods for this.
      >
      > Thanks,
      > Snuyt
      >[/color]


      Comment

      • Herfried K. Wagner [MVP]

        #4
        Re: wait a few seconds

        * Snuyt <snuyt_nospam@s kynet.be> scripsit:[color=blue]
        > I want the program to wait a few seconds between executing code. It
        > should look something like this:[/color]

        \\\
        ....
        System.Threadin g.Thread.Sleep( 2000)
        ....
        ///

        --
        Herfried K. Wagner [MVP]
        <http://www.mvps.org/dotnet>

        Comment

        • Snuyt

          #5
          Re: wait a few seconds

          Tom Leylan wrote:[color=blue]
          > I believe it would help if we got just a bit more information.
          >
          > What is the purpose of the "wait"? The reason it matters is perhaps (and I
          > don't mean anything by it) waiting isn't the thing to do. You want to tie
          > up your application so that it is unable to do anything for 2 seconds? You
          > can put it into a tight loop and watch the clock but nothing (probably) is
          > going to happen while it spins around the loop.
          >
          > Are you waiting for a file to print or a computation to finish? A delay
          > between each attempt to login? You would tend to wait a different way on
          > these things.
          >
          > Tom
          >[/color]

          It 's a card game. The players must have some time to view the cards on
          the table... So the pc can use the cpu to do usefull things (others
          applications than my game), but the game code must wait for a few seconds.

          Snuyt



          [color=blue]
          >
          > "Snuyt" <snuyt_nospam@s kynet.be> wrote in message
          > news:4023e4f6$0 $319$ba620e4c@n ews.skynet.be.. .
          >[color=green]
          >>Hello,
          >>
          >>I want the program to wait a few seconds between executing code. It
          >>should look something like this:
          >>
          >>
          >>public sub xx()
          >>...code...
          >>wait(2) 'wait 2 seconds
          >>...code...
          >>end sub
          >>
          >>
          >>public sub wait(byval secs as integer)
          >>...???...
          >>end sub
          >>
          >>
          >>
          >>Does anyone know how to program this 'waiting'? I've looked for the
          >>timer control, but I don't find the right methods for this.
          >>
          >>Thanks,
          >>Snuyt
          >>[/color]
          >
          >
          >[/color]

          Comment

          • Tom Leylan

            #6
            Re: wait a few seconds

            "Snuyt" <snuyt_nospam@s kynet.be> wrote...
            [color=blue]
            > It 's a card game. The players must have some time to view the cards on
            > the table... So the pc can use the cpu to do usefull things (others
            > applications than my game), but the game code must wait for a few seconds.[/color]

            Ah... then I don't believe you actually need to wait 2 seconds. I could be
            completely wrong of course but generally the person playing should be able
            to stare at the cards for as long as they want to. When they want another
            card they will ask for it.

            On the other hand if a card (or cards) are going to appear automatically
            every 2 seconds then a timer is typically used. When the time elapses an
            event fires and at that point you can make another card appear.

            Again I use the term "generally" but under most circumstances one doesn't
            introduce a multi-second delay... there are other things the card game could
            be doing with the time but if it has nothing to do it will just sit there
            forever if the user doesn't make a move. Other applications will continue
            to operate, you don't have to delay your app for that reason.

            Tom


            Comment

            • Snuyt

              #7
              Re: wait a few seconds

              Tom Leylan wrote:[color=blue]
              > "Snuyt" <snuyt_nospam@s kynet.be> wrote...
              >
              >[color=green]
              >>It 's a card game. The players must have some time to view the cards on
              >>the table... So the pc can use the cpu to do usefull things (others
              >>application s than my game), but the game code must wait for a few seconds.[/color]
              >
              >
              > Ah... then I don't believe you actually need to wait 2 seconds. I could be
              > completely wrong of course but generally the person playing should be able
              > to stare at the cards for as long as they want to. When they want another
              > card they will ask for it.[/color]

              It's a client-server game. The server controls the game, but has to wait
              sometimes for a client to give a card. The delay is needed when all the
              players have played a card on the table. The server must wait 2 seconds
              before clearing the table and giving new cards/sending client a message
              to play a new card. However, the client still can view the most recent
              cards cleared from the table by clicking a button/menu. So the delay is
              pretty usefull in my opinion (because otherwise, the clients ALWAYS have
              to click the button to view the most recent cards played.
              [color=blue]
              >
              > On the other hand if a card (or cards) are going to appear automatically
              > every 2 seconds then a timer is typically used. When the time elapses an
              > event fires and at that point you can make another card appear.
              >
              > Again I use the term "generally" but under most circumstances one doesn't
              > introduce a multi-second delay... there are other things the card game could
              > be doing with the time but if it has nothing to do it will just sit there
              > forever if the user doesn't make a move. Other applications will continue
              > to operate, you don't have to delay your app for that reason.[/color]

              Well, that's the point. The card game doesn't have to do things but to
              wait, and the cpu can process applications meanwhile. That's the kind of
              'wait' I'm looking for.

              threading.threa d.sleep(2000) does the job well, assuming that the
              processor can process other apps meanwhile.
              [color=blue]
              >
              > Tom
              >
              >[/color]

              thx for the time

              Snuyt

              Comment

              • Tom Leylan

                #8
                Re: wait a few seconds

                "Snuyt" <snuyt_nospam@s kynet.be> wrote...
                [color=blue]
                > Well, that's the point. The card game doesn't have to do things but to
                > wait, and the cpu can process applications meanwhile. That's the kind of
                > 'wait' I'm looking for.[/color]

                The additional information helped. You can implement it however you want
                but I (probably) wouldn't implement it that way. By having the thread pause
                you "cannot" have the server do anything but wait... see the difference
                between choosing not to do anything and can't do anything?

                As I read it (and again, ignore it if this makes no sense or doesn't apply)
                you want "2 seconds" to elapse but to conclude that nothing useful can occur
                during that time is an error. You can send any number of messages (from the
                server to the client) in that time. Importantly you could listen for
                messages from the client during that time.

                The server messages could update the clients with time remaining (or
                something more sophisticated). Also two seconds is a little short it seems
                to me. It could be longer and if the server was listening then all the
                players could indicate they were ready and the server would deal right away.
                Or the players could post "chat" to each other. You don't typically want to
                tell the server to just wait and do nothing for two seconds.
                [color=blue]
                > threading.threa d.sleep(2000) does the job well, assuming that the
                > processor can process other apps meanwhile.[/color]

                The server "machine" continues to run and other apps continue to run. The
                thread on your card game server does nothing. I assume it won't be informed
                when one of the players quits out of the game during the time it is asleep.

                You're describing the need for "2 seconds of not dealing cards" not 2
                seconds of being oblivious to the world around it.

                Tom



                Comment

                • Snuyt

                  #9
                  Re: wait a few seconds

                  Tom Leylan wrote:[color=blue]
                  > "Snuyt" <snuyt_nospam@s kynet.be> wrote...
                  >
                  >[color=green]
                  >>Well, that's the point. The card game doesn't have to do things but to
                  >>wait, and the cpu can process applications meanwhile. That's the kind of
                  >>'wait' I'm looking for.[/color]
                  >
                  >
                  > The additional information helped. You can implement it however you want
                  > but I (probably) wouldn't implement it that way. By having the thread pause
                  > you "cannot" have the server do anything but wait... see the difference
                  > between choosing not to do anything and can't do anything?
                  >
                  > As I read it (and again, ignore it if this makes no sense or doesn't apply)
                  > you want "2 seconds" to elapse but to conclude that nothing useful can occur
                  > during that time is an error. You can send any number of messages (from the
                  > server to the client) in that time. Importantly you could listen for
                  > messages from the client during that time.
                  >
                  > The server messages could update the clients with time remaining (or
                  > something more sophisticated). Also two seconds is a little short it seems
                  > to me. It could be longer and if the server was listening then all the
                  > players could indicate they were ready and the server would deal right away.
                  > Or the players could post "chat" to each other. You don't typically want to
                  > tell the server to just wait and do nothing for two seconds.
                  >[/color]


                  You are correct. During the seconds to wait, clients should indeed be
                  able to send chatmessages to the server. But how would you implement the
                  wait then ?

                  Snuyt

                  Comment

                  • José Manuel Agüero

                    #10
                    Re: wait a few seconds

                    Hello, Snuyt:

                    Windows include objects for synchronizing, like mutexes, semaphores, pipes, aces, ...
                    I have never handled them, so I do something like this:

                    \\\
                    TimeToWait=2 'In seconds.
                    StartTime=datet ime.now.ticks
                    do while datetime.now.ti cks<StartTime+T imeToWait*times pan.tickspersec ond
                    System.Threadin g.Thread.Sleep( 100)
                    application.doe vents
                    loop
                    ///

                    Doing it this way, the application seems responsive to the user while being inactive most of the time.
                    I always include a Cancel button that sets to True a flag variable:

                    \\\
                    TimeToWait=2 'In seconds.
                    CancelPressed=f alse
                    StartTime=datet ime.now.ticks
                    do while not CancelPressed andalso _
                    datetime.now.ti cks<StartTime+T imeToWait*times pan.tickspersec ond
                    System.Threadin g.Thread.Sleep( 100)
                    application.doe vents
                    loop

                    sub CancelButton_Cl ick(...) handles ...
                    CancelPressed=t rue
                    end sub
                    ///

                    Regards.


                    "Snuyt" <snuyt_nospam@s kynet.be> escribió en el mensaje news:4023e4f6$0 $319$ba620e4c@n ews.skynet.be.. .
                    | Hello,
                    |
                    | I want the program to wait a few seconds between executing code. It
                    | should look something like this:
                    |
                    |
                    | public sub xx()
                    | ...code...
                    | wait(2) 'wait 2 seconds
                    | ...code...
                    | end sub
                    |
                    |
                    | public sub wait(byval secs as integer)
                    | ...???...
                    | end sub
                    |
                    |
                    |
                    | Does anyone know how to program this 'waiting'? I've looked for the
                    | timer control, but I don't find the right methods for this.
                    |
                    | Thanks,
                    | Snuyt

                    Comment

                    • Cor

                      #11
                      Re: wait a few seconds

                      Hi Snuyt,

                      It is not needed to let your program to sleep for this
                      [color=blue]
                      >It 's a card game. The players must have some time to view the cards on
                      >the table... So the pc can use the cpu to do usefull things (others
                      >applications than my game), but the game code must wait for a few seconds.[/color]

                      This goes automatic.

                      Your code starts again when the users pushes a card or something (an event)

                      When you want to give the users a maximum time that the have to answer, you
                      can use a timer, but 2 seconds is for that of course much to few.

                      Cor


                      Comment

                      • Tom Leylan

                        #12
                        Re: wait a few seconds

                        "Snuyt" <snuyt_nospam@s kynet.be> wrote...
                        [color=blue]
                        > You are correct. During the seconds to wait, clients should indeed be
                        > able to send chatmessages to the server. But how would you implement the
                        > wait then ?[/color]

                        Hi again. You keep describing it as "waiting" ... the server isn't waiting
                        it is "serving" that's all it does. It isn't serving the "deal the cards"
                        message, but it is serving other messages. Look at it this way. If you
                        wanted the server to broadcast the message "it's 10 pm" at 10 o'clock then
                        you would set a timer for 10pm and an event handler would fire that message
                        off. If instead you want a "deal the cards" message to be broadcast 10
                        seconds from now you can set a timer for that.

                        Alternatively (or perhaps in combination) you could put the "game" into a
                        state. It would be in "showing final hands" state. The timer would then
                        reset the state to whatever state was next like "deal the cards state." The
                        point is that the server wouldn't be asleep it would be broadcasting (and
                        receiving) whatever messages were appropriate for "showing final hands"
                        state.

                        You can choose not to broadcast anything of course but you can also choose
                        to send messages about the big tournament the following week or a message
                        that "Eddie has left the game." These messages wouldn't affect the display
                        of the cards on the screen, people can still view them.

                        To answer your question... somehow the server determines when it wants to
                        broadcast the "deal the cards" message then it keeps an eye on the clock.
                        Rather than polling (which would work) an event turns out to be a
                        good/better mechanism.

                        Tom


                        Comment

                        • Cor

                          #13
                          Re: wait a few seconds

                          Hi Snuyt,

                          I think I misreaded it,
                          Than I would just choose in your situation for something as

                          do untilmessagefro muser = true
                          threading.threa d.sleep(100)
                          application.doe vents
                          next

                          Cor


                          Comment

                          • Tom Leylan

                            #14
                            Re: wait a few seconds

                            Perhaps you should read it yet again :-)

                            "Cor" <non@non.com> wrote in message
                            news:uBuhsAo7DH A.488@TK2MSFTNG P12.phx.gbl...[color=blue]
                            > Hi Snuyt,
                            >
                            > I think I misreaded it,
                            > Than I would just choose in your situation for something as
                            >
                            > do untilmessagefro muser = true
                            > threading.threa d.sleep(100)
                            > application.doe vents
                            > next
                            >
                            > Cor
                            >
                            >[/color]


                            Comment

                            • Cor

                              #15
                              Re: wait a few seconds

                              Hi Snuyt,

                              I readed it again, I became confused by the server in the thread, but now I
                              saw it was from Tom, so this is not necessary.

                              Sorry, but I became confused by this thread.

                              You do not need a clock, you do not need a wait, just events to which wait
                              on the click of the user and do than the things which are needed.


                              Cor[color=blue]
                              >
                              > do untilmessagefro muser = true
                              > threading.threa d.sleep(100)
                              > application.doe vents
                              > next[/color]


                              Comment

                              Working...