473,322 Members | 1,538 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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 22045
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Rasmus Grøndahl Olsen | last post by:
I have tried to write a wait function but it seems like it will not brake the while loop. I tried two different solutions. Can anyone tell me what I am doing wrong, and come with another...
31
by: Stephanie | last post by:
I have a newbie question (couldn't find the answer with google) How to show countdown from 10 to 0 seconds. Stephanie
5
by: Erwin Kloibhofer | last post by:
what if i have a webpage that displays the text "please wait, this may take a few seconds..." and it now waits until some event on the server happens. whatever this is, this can be quick, but it...
4
by: Pierpaolo | last post by:
hi!! Is there a function in order to wait for a time in milliseconds?? Thanks in advance
16
by: Thirsty Traveler | last post by:
I would like to create a test harness that simulates multiple concurrent users executing an individual thread. I would like this to be determined at runtime when the user specifies the number of...
2
by: bigpoppa | last post by:
Hey I need help (doesn't everyone?) on a script. The script functions like this: Please wait X seconds for download (for example x=15 seconds) Please wait 14 seconds for download (and then a second...
4
by: MrDeej | last post by:
Hello guys! I am trying to use trevors wait function on my odbc database for a countdown function. But this function is not stable as it sometimes dosent actually wait before launching next line...
40
by: =?Utf-8?B?Um9iZXJ0IEUuIEZsYWhlcnR5?= | last post by:
What is the C# command to wait for a specified period of time? I am writing a windows service that will process a file once it has beed created or changed. I'm using the fileSystemWatcher to...
5
by: Jeremy | last post by:
Hi all, I have database actions that will potentially take several seconds to complete. My normal page uses AJAX so keeping the user informed of what is happening is not a problem. ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.