-
Nov 11th, 2008, 04:51 PM
#1
Thread Starter
Junior Member
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?
-
Nov 11th, 2008, 04:54 PM
#2
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
-
Nov 11th, 2008, 04:58 PM
#3
Thread Starter
Junior Member
Re: VB6 Sleep Function
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
-
Nov 11th, 2008, 05:17 PM
#4
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.
-
Nov 11th, 2008, 07:40 PM
#5
Re: VB6 Sleep Function
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??
-
Nov 12th, 2008, 12:40 AM
#6
Re: VB6 Sleep Function
CDRIVE, That looks good to overcome the situation when pass midnight, however that has some minor problems:
- 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
- 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
-
Nov 12th, 2008, 08:55 AM
#7
Re: VB6 Sleep Function
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??
-
Nov 12th, 2008, 09:05 AM
#8
Re: VB6 Sleep Function
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??
-
Oct 4th, 2014, 08:47 PM
#9
New Member
Re: VB6 Sleep Function
Many thanks to LaVolpe for this posting of a sleep function. I needed a way to add a pause to a standard code module and the API call is exactly what the doctor ordered.
-
Oct 5th, 2014, 04:16 AM
#10
Re: VB6 Sleep Function
The problem with DoEvents is that cause the Vb5/6 runtime to run any other event. Vb6 have re-entrance in event subroutine, so a pause in any of these subroutines or subroutines in a "bas" module, that we call from event subroutines, can't guarantee that a pause has a meaning of a pause.
So we have to break the re-entrance. I use a static variable once:
Sub That_Event()
static once as boolean
if once then exit sub
once=true
' our code here
once=false
exit sub
We can use a public "donothing" as boolean, so if it is true no event subroutine can do anything...until our pause end.
-
Nov 3rd, 2014, 04:34 PM
#11
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
-
Feb 19th, 2019, 12:43 PM
#12
Re: VB6 Sleep Function
Just playing around and saw all the answers. Just throwing this out:
Code:
Private Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long)
Private Sub Command1_Click()
Dim iTestIt As Integer
iTestIt = 1
Dim i As Integer
For i = 1 To 10
Text1.Text = iTestIt
Text1.Refresh
Sleep 1000
iTestIt = iTestIt + 1
Next i
End Sub
Please remember next time...elections matter!
-
Feb 19th, 2019, 01:50 PM
#13
Re: VB6 Sleep Function
the vb.timer is quite nice, as it will not freeze the form.
it all depends what u need it for and what kind of state u want the form to have while "processing" whatever you are doing.
before working with game-loops, i used the vb.timer.
its easy to make the timer do "operations", recursively, example:
Code:
Dim operation as integer
Dim funpr1 as integer
Private Sub doOperation(ByVal Op&)
' something '
End Sub
Private Sub function1()
funpr1 = funpr1 + 1
doOperation funpr1
If funpr1 = 10 Then operation = 0
End Sub
Private Sub Timer1_Timer()
Select Case operation
Case 0: ' nothing '
Case 1: function1
Case 2: function2
Case 3: function3
End Select
End Sub
using sleep it will freeze the form and the functions, halting your program.
better to avoid using sleep if possible.
-
Feb 19th, 2019, 04:33 PM
#14
Re: VB6 Sleep Function
This thread has been started more than a decade ago!
-
Feb 19th, 2019, 05:06 PM
#15
Re: VB6 Sleep Function
It started, went dormant for six years, revived for a brief time, went dormant for another five years....see y'all back here again around 2024 or 2025.
My usual boring signature: Nothing
-
Feb 20th, 2019, 07:11 AM
#16
Re: VB6 Sleep Function
Originally Posted by dee-u
This thread has been started more than a decade ago!
Did you not see the post title...it obviously worked
Please remember next time...elections matter!
-
Feb 20th, 2019, 07:22 AM
#17
Re: VB6 Sleep Function
Originally Posted by Shaggy Hiker
It started, went dormant for six years, revived for a brief time, went dormant for another five years....see y'all back here again around 2024 or 2025.
Someone has to reduce the value they are passing to the Sleep API in this thread to resemble a form of sane communication :-))
cheers,
</wqw>
-
Feb 20th, 2019, 11:08 AM
#18
My usual boring signature: Nothing
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
|