|
-
Dec 12th, 2006, 05:56 AM
#1
Thread Starter
Member
Wait function
I am looking for the best way to freeze a function for a certain amount of time. I can do a loop with a counter but thats a bit sloppy and generates lag. In c-script we had a "wait" function that we could use to pause the function for a certain amount of time... is there anything like that in basic? Ive tried this with a var counter and using time but the loops lag the mouse and program out a little.
-
Dec 12th, 2006, 05:59 AM
#2
Re: Wait function
Use the Sleep() API:
VB Code:
Option Explicit
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Sub Form_Load()
Sleep 3000 'will "sleep" 3 seconds
End Sub
-
Dec 27th, 2006, 08:37 PM
#3
Thread Starter
Member
Re: Wait function
That is ok by I am having issues with sleep. Maybe someone can help. Here is a example:
Option Explicit
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Sub Command1_Click()
sleeper
SendKeys "Hello"
sleeper2
SendKeys "10 sec"
End Sub
Private Sub sleeper()
Sleep (60000)
End Sub
Private Sub sleeper2()
Sleep (10000)
End Sub
What happens is I get Hello and 10 sec at same time. Please help.. not sure what could be wrong but I tested in 100 different ways I think.
-
Dec 27th, 2006, 09:27 PM
#4
Re: Wait function
It really shouldn't be at the same time.... if your program is getting the text that your sending then it might appear to be the same time as sleep stops everything in your program for the selected time, so it would appear that the text is sent after each other as there is nothing working in your program to recieve the first text before the second is displayed.
If you wanted something that wouldn't halt your whole program you can use
VB Code:
Option Explicit
Private Declare Function GetTickCount Lib "kernel32" () As Long
Private Sub Command1_Click()
Sleep 6
SendKeys "Hello"
Sleep 10
SendKeys "10 sec"
End Sub
Sub Sleep(Seconds As Long)
Dim tmr As Long
'Our finish time
tmr = GetTickCount + (Seconds * 1000)
'If our current time is less than the finish, then doevents
While GetTickCount < tmr
DoEvents
Wend
End Sub
-
Dec 27th, 2006, 09:56 PM
#5
Re: Wait function
Sleep or SleepEx will suspend execution of the entire program.
Doing something like Andrew did is probably what Christian looking for but would stringly recommend NOT to use While - Wend type of loop - even MS recommends not to use it.
Anyway, here is my Wait function:
VB Code:
Option Explicit
Private Sub Command1_Click()
Debug.Print Time
Wait 3 '3 seconds
Debug.Print Time
End Sub
Public Sub Wait(seconds As Integer)
Dim dTimer As Double
dTimer = Timer
Do While Timer < dTimer + seconds
DoEvents
Loop
End Sub
-
Dec 27th, 2006, 10:11 PM
#6
Re: Wait function
Just wondering why shouldn't you use them?
Edit: Found it - http://msdn2.microsoft.com/en-us/library/cbe735w2.aspx
Last edited by Andrew G; Dec 27th, 2006 at 10:15 PM.
-
Dec 27th, 2006, 10:16 PM
#7
Re: Wait function
While - Wend was designed for QBasic yet (if I am not mistaken) but the problem is that it doesn't allow easy exit from the loop (like Exit Do or Exit For).
-
Dec 28th, 2006, 05:08 AM
#8
Thread Starter
Member
Re: Wait function
Works great! I appreciate your help. Confused a little but about sleep though. Not sure why my active window wasnt getting the sendkeys until both instances of sleep was over. Regardless.. changing debug to sendkeys does it. Thanks again.
-
Dec 28th, 2006, 05:11 AM
#9
Re: Wait function
 Originally Posted by christiangibbs
... Not sure why my active window wasnt getting the sendkeys until both instances of sleep was over...
Sleep does that. Your application won't react in any way until Sleep is over - code won't execute, you won't be able to fire events, Timers won't work... it stops your application for the time.
-
Mar 15th, 2007, 02:01 PM
#10
New Member
Re: Wait function
you guys just saved my sanity.... thanks very much, i realised what sleep was doing but couldnt work out an alternative.
-
Mar 16th, 2007, 05:01 AM
#11
Addicted Member
Re: Wait function
And here is another wait function ... a better one.
Note that I have observed that using Timer directly can result in bad return values. I have notified MS. *** Also, the other code does not take into account passing through midnight *** and may (will) fail ... Common mistakes.
I know this code is longer and more "complex", but it addresses the required issues. Most people don't care, but should. However, some - that want code to work all the time and not vicariously - will care.
Public Sub MyWait(ByVal pasSeconds As Single)
Dim locTimerStart As Single
Dim locTimerSample As Single
Dim locElapsed As Single
locTimerStart = Timer
Do
DoEvents
locTimerSample = Timer
locElapsed = locTimerSample - locTimerStart
If locElapsed < 0 Then locElapsed = locElapsed + 86400
Loop While locElapsed < pasSeconds
End Sub
Last edited by Jay Rogozinsky; Mar 16th, 2007 at 05:54 AM.
-
Mar 16th, 2007, 12:33 PM
#12
Re: Wait function
Code:
Option Explicit
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Sub Command1_Click()
SendKeys "Hello"
DoEvents'nothing happens until after sleeper2 returns without this
sleeper2
SendKeys "10 sec"
End Sub
Private Sub sleeper()
Sleep (60000)
End Sub
Private Sub sleeper2()
Sleep (10000)
End Sub
The most difficult part of developing a program is understanding the problem.
The second most difficult part is deciding how you're going to solve the problem.
Actually writing the program (translating your solution into some computer language) is the easiest part.
Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.
Please Help Us To Save Ana
-
Mar 19th, 2007, 07:43 AM
#13
Re: Wait function
 Originally Posted by Jay Rogozinsky
And here is another wait function ... a better one.
But won't your code send the CPU to 99%...:s
Also, not to mention the fact that coding DoEvents like this also requires a lot more coding to stop the user closing the UI and so on.
Woka
-
Mar 19th, 2007, 07:54 AM
#14
Re: Wait function
 Originally Posted by Wokawidget
But won't your code send the CPU to 99%...:s
Also, not to mention the fact that coding DoEvents like this also requires a lot more coding to stop the user closing the UI and so on.
Woka
... and I will second that opinion.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|