Results 1 to 14 of 14

Thread: Wait function

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2006
    Posts
    36

    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.

  2. #2
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: Wait function

    Use the Sleep() API:
    VB Code:
    1. Option Explicit
    2.     Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    3.  
    4. Private Sub Form_Load()
    5.     Sleep 3000 'will "sleep" 3 seconds
    6. End Sub

  3. #3

    Thread Starter
    Member
    Join Date
    Dec 2006
    Posts
    36

    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.

  4. #4
    Frenzied Member Andrew G's Avatar
    Join Date
    Nov 2005
    Location
    Sydney
    Posts
    1,587

    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:
    1. Option Explicit
    2. Private Declare Function GetTickCount Lib "kernel32" () As Long
    3.  
    4. Private Sub Command1_Click()
    5. Sleep 6
    6.  
    7. SendKeys "Hello"
    8.  
    9. Sleep 10
    10.  
    11. SendKeys "10 sec"
    12. End Sub
    13.  
    14. Sub Sleep(Seconds As Long)
    15. Dim tmr As Long
    16.  
    17.     'Our finish time
    18.     tmr = GetTickCount + (Seconds * 1000)
    19.     'If our current time is less than the finish, then doevents
    20.     While GetTickCount < tmr
    21.         DoEvents
    22.     Wend
    23. End Sub

  5. #5
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    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:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4.     Debug.Print Time
    5.    
    6.     Wait 3 '3 seconds
    7.    
    8.     Debug.Print Time
    9. End Sub
    10.  
    11. Public Sub Wait(seconds As Integer)
    12. Dim dTimer As Double
    13.  
    14.     dTimer = Timer
    15.     Do While Timer < dTimer + seconds
    16.         DoEvents
    17.     Loop
    18.  
    19. End Sub

  6. #6
    Frenzied Member Andrew G's Avatar
    Join Date
    Nov 2005
    Location
    Sydney
    Posts
    1,587

    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.

  7. #7

  8. #8

    Thread Starter
    Member
    Join Date
    Dec 2006
    Posts
    36

    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.

  9. #9
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: Wait function

    Quote 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.

  10. #10
    New Member
    Join Date
    Mar 2007
    Posts
    8

    Re: Wait function

    you guys just saved my sanity.... thanks very much, i realised what sleep was doing but couldnt work out an alternative.

  11. #11
    Addicted Member
    Join Date
    Jul 2001
    Posts
    133

    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.

  12. #12
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    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

  13. #13
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: Wait function

    Quote 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

  14. #14
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Wait function

    Quote 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
  •  



Click Here to Expand Forum to Full Width