VB6 Sleep Function-VBForums
Results 1 to 8 of 8

Thread: VB6 Sleep Function

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2008
    Posts
    29

    VB6 Sleep Function

    The sleep function for my VB6 doesn't work, it just freezes up the application. Can anyone give me a sleep function that won't freeze up the application?

  2. #2
    VBaholic & Loving It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    GetWindowRect()
    Posts
    11,938

    Re: VB6 Sleep Function

    How about the API?
    In your delcarations section add this
    Code:
    Private Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long)
    To use it
    Code:
     Sleep 1000 ' to sleep for 1 second
    Insomnia is just a byproduct of, "It can't be done"

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum. Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts


    {Alpha Image Control} {Memory Leak FAQ} {GDI+ Classes/Samples} {Unicode Open/Save Dialog} {Icon Organizer/Extractor}
    {VBA Control Arrays} {XP/Vista Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jan 2008
    Posts
    29

    Re: VB6 Sleep Function

    Quote Originally Posted by LaVolpe
    How about the API?
    In your delcarations section add this
    Code:
    Private Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long)
    To use it
    Code:
     Sleep 1000 ' to sleep for 1 second
    Yeah, I was talking about that code, the only problem I have after that is that it doesn't seem to want to do all the codes after that sleep function

  4. #4
    VBaholic & Loving It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    GetWindowRect()
    Posts
    11,938

    Re: VB6 Sleep Function

    Then something else is happening. Sleep only pauses your code then the next lines continue on. You may want to be more specific and post some relevant code that we can look at.
    Insomnia is just a byproduct of, "It can't be done"

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum. Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts


    {Alpha Image Control} {Memory Leak FAQ} {GDI+ Classes/Samples} {Unicode Open/Save Dialog} {Icon Organizer/Extractor}
    {VBA Control Arrays} {XP/Vista Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5
    PowerPoster CDRIVE's Avatar
    Join Date
    Jul 2007
    Posts
    2,451

    Re: VB6 Sleep Function

    Quote Originally Posted by Derkel
    The sleep function for my VB6 doesn't work, it just freezes up the application. Can anyone give me a sleep function that won't freeze up the application?
    That's because the Sleep API freezes all events and the next line of code in your app until the Sleep period has expired. By it self I find it nearly useless. My favorite sub incorporates the Sleep API (in 2mS increments) and some additional code to create the Pause sub. Just use Pause (Sec or fraction of a second) where you would use Sleep.
    Code:
    Option Explicit
    Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    
    ' Credits: (Milk (Sleep+Pause Sub)). (Wayne Spangler (Pause Sub))
    Private Sub Pause(ByVal Delay As Single)
       Delay = Timer + Delay
       If Delay > 86400 Then 'more than number of seconds in a day
          Delay = Delay - 86400
          Do
              DoEvents ' to process events.
              Sleep 1 ' to not eat cpu
          Loop Until Timer < 1
       End If
       Do
           DoEvents ' to process events.
           Sleep 1 ' to not eat cpu
       Loop While Delay > Timer
    End Sub
    <--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
    If topic has been resolved, please pull down the Thread Tools & mark it Resolved.


    Is VB consuming your life, and is that a bad thing??

  6. #6
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: VB6 Sleep Function

    CDRIVE, That looks good to overcome the situation when pass midnight, however that has some minor problems:
    1. As Timer() never reaches 86400, if Delay = 86400 (rare) then only the second Do...Loop runs 1 round: approx only 1 millisecond delay.
      So, instead of If Delay > 86400 Then , that should be If Delay >= 86400 Then
    2. Delay may be greater than multiple of 86400, such as Delay = 259210 (=3*86400+10), then after Delay = Delay - 86400 you still have Delay > 86400.

    Below is my Pause() function, it uses only one Do...Loop.
    As Timer() is only updated every 1/64 sec = 15.625 millisecs, you can give the loop sleep a bit longer before checking Timer() vs TimeOut.
    If higher-resolution timing is required then Timer() is not good enough, perhaps we have to use something else such as QueryPerformanceCounter()
    Code:
    Option Explicit
    
    Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    
    Public Sub Pause(SecsDelay As Single)
       Dim TimeOut   As Single
       Dim PrevTimer As Single
       
       PrevTimer = Timer
       TimeOut = PrevTimer + SecsDelay
       Do While PrevTimer < TimeOut
          Sleep 4 '-- Timer is only updated every 1/64 sec = 15.625 millisecs.
          DoEvents
          If Timer < PrevTimer Then TimeOut = TimeOut - 86400 '-- pass midnight
          PrevTimer = Timer
       Loop
    End Sub
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    Baby Steps a guided tour
    IsDigits() and IsNumber() functions Wichmann-Hill Random() function >> and << functions for VB CopyFileByChunk

  7. #7
    PowerPoster CDRIVE's Avatar
    Join Date
    Jul 2007
    Posts
    2,451

    Re: VB6 Sleep Function

    Quote Originally Posted by LaVolpe
    Then something else is happening. Sleep only pauses your code then the next lines continue on. You may want to be more specific and post some relevant code that we can look at.
    Ah, I thought the TS was referring to what's happening, or not happing, in other events during the Sleep interval.
    <--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
    If topic has been resolved, please pull down the Thread Tools & mark it Resolved.


    Is VB consuming your life, and is that a bad thing??

  8. #8
    PowerPoster CDRIVE's Avatar
    Join Date
    Jul 2007
    Posts
    2,451

    Re: VB6 Sleep Function

    Quote Originally Posted by anhn
    CDRIVE, That looks good to overcome the situation when pass midnight, however that has some minor problems:
    [LIST=1][*]As Timer() never reaches 86400, if Delay = 86400 (rare) then only the second Do...Loop runs 1 round: approx only 1 millisecond delay.
    Anhn, thanks for pointing that out. Even though I can't imagine a delay >86400 it's still worth mentioning.
    <--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
    If topic has been resolved, please pull down the Thread Tools & mark it Resolved.


    Is VB consuming your life, and is that a bad thing??

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.