Visual Basic Code , VB.NET Code, VB Code
  Home   :  Code   :  Forums   :  Submit   :  Mailing List   :  About   :  Contact


Quick Basic Sleep Statement Equivilent?


Quick Basic Sleep Statement Equivilent?

Author
Message
CDRIVE
CDRIVE
Forum God
Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)

Group: Forum Members
Posts: 548, Visits: 2.6K
I've searched all the Visual Studio Key Words but can't find a simple equivilent to the Quick Basic "SLEEP" function. I've looked at Timer controls and the "Sec" functions hoping to use one of them to replace it? I thought I might be able to use one of them with the "Stop" function, but it appears that the Stop function is only used within the VB environment for debugging. I wouldn't know how to resume execution anyway.

Please disregard the above questions & statements. Every time I think I finally have a good grasp of VB's event driven programming structure I forget and revert back to a QB mind set. I realize now that I simply need to write my code within the Timer event.   

Thanks

CDRIVE

________________________________________________________________ 

"So much to learn. So little time to do it. Wise men know it's later than one thinks"!

Mark's Syntax.Zip    Pause Sub

I don't answer programming questions via PMs. That's what the forum is for! 

Edited
2/4/2007 by CDRIVE
Keithuk
Keithuk
Forum God
Forum God (298K reputation)

Group: Moderators
Posts: 1.9K, Visits: 5.5K
Well the SLEEP call in Qbasic stops for a number of seconds

SLEEP 10 will stop for 10 seconds. If the number is omitted or 0 then it will stop until a key is pressed.

There is a Sleep API call in VB.

Private Declare Sub Sleep Lib "kernel32.dll" _
 (ByVal dwMilliseconds As Long)
Private Declare Function SleepEx Lib "kernel32.dll" _
(ByVal dwMilliseconds As LongByVal bAlertable As LongAs Long

But when Sleep is used in a VB app everything stops everything for the specfic amount of time in Milliseconds. You have to put DoEvents either side. Wink

Keith

I've been programming with VB for 17 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.

CDRIVE
CDRIVE
Forum God
Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)

Group: Forum Members
Posts: 548, Visits: 2.6K
Thanks for the API info but I doubt I'll be going that rout. I don't want everything stoping while my code is executing. Below is a QB code block that prints 1 through 10. It prints it one number at a time with 1 second pauses between each print cycle through the loop. I want to reproduce this basic concept in VB but I'm not having much sucess using the Timer control to do it. I think I need two Timer controls?

Thanks

CDRIVE


CLS
DIM Check, Counter
Check = True: Counter = 0       ' Initialize variables.

DO   ' Outer loop.
   DO WHILE Counter < 20        ' Inner loop.
      Counter = Counter + 1     ' Increment Counter.
      SLEEP 1                   ' Wait 1 second between each print statement.
      PRINT Counter             ' Print each count
      IF Counter = 10 THEN      ' If condition is True.
      Check = False             ' Set value of flag to False.
      EXIT DO                   ' Exit inner loop.
      END IF
   LOOP
LOOP UNTIL Check = False        ' Exit outer loop immediately.

END


 

________________________________________________________________ 

"So much to learn. So little time to do it. Wise men know it's later than one thinks"!

Mark's Syntax.Zip    Pause Sub

I don't answer programming questions via PMs. That's what the forum is for! 

Mark
Mark
Forum God
Forum God (141K reputation)

Group: Moderators
Posts: 1.1K, Visits: 11K
See if this does it for you

Option Explicit
Dim i As Integer

Private Sub Form_Load()
    Timer1.Interval = 1000
End Sub

Private Sub Timer1_Timer()
    i = i + 1
    Me.Caption = i
    
    If i = 10 Then
        Timer1.Enabled = False
    End If
End Sub

waynespangler
waynespangler
Forum God
Forum God (97K reputation)Forum God (97K reputation)Forum God (97K reputation)Forum God (97K reputation)Forum God (97K reputation)Forum God (97K reputation)Forum God (97K reputation)Forum God (97K reputation)Forum God (97K reputation)

Group: Forum Members
Posts: 910, Visits: 5.4K
Try writing your own delay if the sleep is not what you want. Try this:
Option Explicit

Dim counter As Integer
Dim check As Boolean

Private Sub Command1_Click()
    Do   ' Outer loop.
       Do While counter < 20        ' Inner loop.
          counter = counter + 1     ' Increment Counter.
          Sleep 1                   ' Wait 1 second between each print statement.
          'changed your output some
          Text1 = Text1 & counter   ' Print each count
          If counter = 10 Then      ' If condition is True.
          check = False             ' Set value of flag to False.
          Exit Do                   ' Exit inner loop.
          End If
       Loop
    Loop Until check = False        ' Exit outer loop immediately.
End Sub

'add a delay time to the current time and waits for the current
'time to catch up.
Private Sub Sleep(ByVal delay As Single)
    Dim x As Single
    x = Timer + delay
    Do While x > Timer
        DoEvents
    Loop
End Sub

You pass the sleep a single number so if you want it to delay .1 sec you can pass it .1

Hope this helps.

Wayne

I know enough to be dangerious.

It is hard to understand how a cemetery can raise its burial rates and blame it on the cost of living.

CDRIVE
CDRIVE
Forum God
Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)

Group: Forum Members
Posts: 548, Visits: 2.6K
Wayne, I took your Sleep Sub and ran with it. Oh yeaaaaaaaaaa! I added a few required things like the Print text1 and defined text1 as a String. When I got it to run I was surprised to see that the print out printed 1, 12, 123, 1234, and so on. It was not exactly what I wanted but I found it interesting. I'm sure I'll find a use for that output format in another project someday. Anyway, I messed with the code a bit more and ended up with the code below. Thank you sooooo much for your time and skill. It works great!!

'_____________________________________________________________________________
'Some of this code provided by Wayne Spangler of a1vbcode.com. He specificaly supplied 'the Private Sub Sleep block which is the heart of the timing sequence.
'_________________________________________________________________________________________

Option Explicit
Dim Display As String
Dim Prompt As String
Dim Counter As Integer
Dim Check As Boolean

Private Sub Command1_Click()
    Do   ' Outer loop.
       Do While Counter < 20        ' Inner loop.
          Counter = Counter + 1     ' Increment Counter.
          Sleep 1                  ' Wait 1 second between each print statement.
          Display = Prompt & Counter   ' Print each count
          Print Display
          If Counter = 10 Then      ' If condition is True.
          Check = False             ' Set value of flag to False.
          Exit Do                   ' Exit inner loop.
          End If
       Loop
    Loop Until Check = False        ' Exit outer loop immediately.
End Sub

'add a delay time to the current time and waits for the current
'time to catch up.
Private Sub Sleep(ByVal delay As Single)
    Dim x As Single
    x = Timer + delay
    Do While x > Timer
        DoEvents
    Loop
End Sub

Private Sub Form_Load()
Show
Prompt = "Count  "                     'Prefix Print Statement with Count.
End Sub


________________________________________________________________ 

"So much to learn. So little time to do it. Wise men know it's later than one thinks"!

Mark's Syntax.Zip    Pause Sub

I don't answer programming questions via PMs. That's what the forum is for! 

Keithuk
Keithuk
Forum God
Forum God (298K reputation)

Group: Moderators
Posts: 1.9K, Visits: 5.5K
CDRIVE (2/5/2007)

Sleep 1                  ' Wait 1 second between each print statement.

Sleep 1 doesn't wait for 1 second not in VB. It will wait for 1 thousandth of a second which you won't notice. Wink

Keith

I've been programming with VB for 17 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.

CDRIVE
CDRIVE
Forum God
Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)

Group: Forum Members
Posts: 548, Visits: 2.6K
Keith, what can I say? I'm the novice here. I only know what I see. What I see is the following:

Sleep = 1     ' Print statement (prints) executes every second.

Sleep = 0.5  ' Print statement (prints) executes every 500mS.

Sleep = 60   ' Print statement (prints) executes every minute.

Wayne's code is simulating the QB SLEEP statement perfectly! I wish I could somehow register "Sleep" with VB and call it like any other VB statement. I'll register it as a Sub though.

Thanks,

Chris

________________________________________________________________ 

"So much to learn. So little time to do it. Wise men know it's later than one thinks"!

Mark's Syntax.Zip    Pause Sub

I don't answer programming questions via PMs. That's what the forum is for! 

Keithuk
Keithuk
Forum God
Forum God (298K reputation)

Group: Moderators
Posts: 1.9K, Visits: 5.5K
CDRIVE (2/6/2007)
Keith, what can I say? I'm the novice here. I only know what I see. What I see is the following:

Sleep = 1     ' Print statement (prints) executes every second.

Sleep = 0.5  ' Print statement (prints) executes every 500mS.

Sleep = 60   ' Print statement (prints) executes every minute.

Wayne's code is simulating the QB SLEEP statement perfectly! I wish I could somehow register "Sleep" with VB and call it like any other VB statement. I'll register it as a Sub though.

Well if you look at the code again Chris what I see is no =

Wayne's code isn't simulating the GB Sleep because he is using Option Explicit and a Sleep Sub not the Sleep API.

Keith

I've been programming with VB for 17 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.

Edited
2/7/2007 by Keithuk
CDRIVE
CDRIVE
Forum God
Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)Forum God (103K reputation)

Group: Forum Members
Posts: 548, Visits: 2.6K
Perhaps as a neophyte I use the wrong terms? I know that I liked the Sleep statement in QB an used it for effects. Wayne's code enabled me to create a variable named Sleep and use it in much the same way & location that it would be in QB. I didn't use the API call that you suggested because you said that it stops everything on the computer for the specified time. Perhaps I misunderstood you.

Thanks,

Chris

________________________________________________________________ 

"So much to learn. So little time to do it. Wise men know it's later than one thinks"!

Mark's Syntax.Zip    Pause Sub

I don't answer programming questions via PMs. That's what the forum is for! 

GO


Similar Topics


Reading This Topic


Login
Existing Account
Email Address:


Password:


Social Logins

Select a Forum....

















A1VBCode Forums


Search