469,602 Members | 2,143 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,602 developers. It's quick & easy.

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

Nov 20 '05 #1
15 21092
Cor
Hallo Snuyt

threading.thread.sleep(2000)

Cor
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

Nov 20 '05 #2
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" <sn**********@skynet.be> wrote in message
news:40*********************@news.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

Nov 20 '05 #3
* Snuyt <sn**********@skynet.be> scripsit:
I want the program to wait a few seconds between executing code. It
should look something like this:


\\\
....
System.Threading.Thread.Sleep(2000)
....
///

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #4
Tom Leylan wrote:
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

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


"Snuyt" <sn**********@skynet.be> wrote in message
news:40*********************@news.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



Nov 20 '05 #5
"Snuyt" <sn**********@skynet.be> wrote...
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.


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
Nov 20 '05 #6
Tom Leylan wrote:
"Snuyt" <sn**********@skynet.be> wrote...

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.

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.


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.

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.
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.thread.sleep(2000) does the job well, assuming that the
processor can process other apps meanwhile.

Tom


thx for the time

Snuyt

Nov 20 '05 #7
"Snuyt" <sn**********@skynet.be> wrote...
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.
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.
threading.thread.sleep(2000) does the job well, assuming that the
processor can process other apps meanwhile.


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

Nov 20 '05 #8
Tom Leylan wrote:
"Snuyt" <sn**********@skynet.be> wrote...

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.

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.

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

Nov 20 '05 #9
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=datetime.now.ticks
do while datetime.now.ticks<StartTime+TimeToWait*timespan.t ickspersecond
System.Threading.Thread.Sleep(100)
application.doevents
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=false
StartTime=datetime.now.ticks
do while not CancelPressed andalso _
datetime.now.ticks<StartTime+TimeToWait*timespan.t ickspersecond
System.Threading.Thread.Sleep(100)
application.doevents
loop

sub CancelButton_Click(...) handles ...
CancelPressed=true
end sub
///

Regards.
"Snuyt" <sn**********@skynet.be> escribió en el mensaje news:40*********************@news.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

Nov 20 '05 #10
Cor
Hi Snuyt,

It is not needed to let your program to sleep for this
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.


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
Nov 20 '05 #11
"Snuyt" <sn**********@skynet.be> wrote...
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 ?


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
Nov 20 '05 #12
Cor
Hi Snuyt,

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

do untilmessagefromuser = true
threading.thread.sleep(100)
application.doevents
next

Cor
Nov 20 '05 #13
Perhaps you should read it yet again :-)

"Cor" <no*@non.com> wrote in message
news:uB*************@TK2MSFTNGP12.phx.gbl...
Hi Snuyt,

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

do untilmessagefromuser = true
threading.thread.sleep(100)
application.doevents
next

Cor

Nov 20 '05 #14
Cor
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

do untilmessagefromuser = true
threading.thread.sleep(100)
application.doevents
next

Nov 20 '05 #15
Cor wrote:
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
do untilmessagefromuser = true
threading.thread.sleep(100)
application.doevents
next



Ok, thanks all for the support.

Snuyt

Nov 20 '05 #16

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Rasmus Grøndahl Olsen | last post: by
31 posts views Thread by Stephanie | last post: by
5 posts views Thread by Erwin Kloibhofer | last post: by
4 posts views Thread by Pierpaolo | last post: by
16 posts views Thread by Thirsty Traveler | last post: by
40 posts views Thread by =?Utf-8?B?Um9iZXJ0IEUuIEZsYWhlcnR5?= | last post: by
5 posts views Thread by Jeremy | last post: by
reply views Thread by guiromero | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.