[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 776
  • Last Modified:

Programmed pause in VB6

I've asked this before, but never had success.  In a VB6 form, I'd like to put values into each of 5 text boxes with a 1 second pause after each - as in:
 
106     Form1.txtSeed1.Text = Seed1Text
108     Form1.txtSeed1.Tag = Seed1Tag
         'Pause 1 sec.
110     Form1.txtSeed5.Text = Seed5Text
112     Form1.txtSeed5.Tag = Seed5Tag
        'Pause 1 sec.
114     Form1.txtSeed13.Text = Seed13Text
116     Form1.txtSeed13.Tag = Seed13Tag
        'Pause 1 sec.
118     Form1.txtSeed21.Text = Seed21Text
120     Form1.txtSeed21.Tag = Seed21Tag
        'Pause 1 sec.
122     Form1.txtSeed25.Text = Seed25Text
124     Form1.txtSeed25.Tag = Seed25Tag

I've been advised to make a call to the 'Sleep API', but not clearly enough.  Do I make a single call, or one for every pause?  Do I create a Public module?  Do I need 'DoEvent' along with Sleep(1000) ?  Can someone please show how this 1 second delay would best work with my code?
0
wjshore
Asked:
wjshore
  • 3
  • 3
  • 2
  • +5
2 Solutions
 
WikkardCommented:
Calling Sleep will completely block your program, where as doevents (even in a loop) will actually allow it to continue responding to events.

Depending on your requirements you could use either;
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Sleep 1000

or call doevents in a loop

0
 
WikkardCommented:
By a loop I mean check for the elapsed time,

finished = false
starttime = now
do while (not finished)
  elapsed = datediff("s",starttime, now)
  if elapsed >=60 then finished = true
  doevents
loop
0
 
wjshoreAuthor Commented:
I appreciate everyone's time/help.  I'm sorry, but a bit more hand-holding is required.  Please begin with my code and show EXACTLY how the delay could be implemented.  Thanks.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
hesCommented:
'At the top of your form in the declaration section put
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

'Your Code
106     Form1.txtSeed1.Text = Seed1Text
108     Form1.txtSeed1.Tag = Seed1Tag
         Sleep(1000)
110     Form1.txtSeed5.Text = Seed5Text
112     Form1.txtSeed5.Tag = Seed5Tag
         Sleep(1000)
114     Form1.txtSeed13.Text = Seed13Text
116     Form1.txtSeed13.Tag = Seed13Tag
         Sleep(1000)
118     Form1.txtSeed21.Text = Seed21Text
120     Form1.txtSeed21.Tag = Seed21Tag
         Sleep(1000)
122     Form1.txtSeed25.Text = Seed25Text
124     Form1.txtSeed25.Tag = Seed25Tag
0
 
wjshoreAuthor Commented:
OK, I feel like an idiot.  I've misled people trying to help.  The code is NOT in a form - it simply refers to a form (form1).  The code is in a module which is called to fill the form's text boxes.  How does this change the correct answer?  Please accept my appology.
0
 
WikkardCommented:
The answer should be the same
:-)
0
 
wjshoreAuthor Commented:
Nope.  With the Private Declare in the module's General Declarations section and the code listed as suggested, the program hangs.
0
 
TreyHCommented:
If you need your program to pause but not freeze, you can use the 'SetWaitableTimer"

Here's a link to explain:
http://support.microsoft.com/kb/231298

I have a module I keep around and just add to my projects when needed. Here's the code:

'------------------------------------- Start Paste
Option Explicit

Private Type FILETIME
    dwLowDateTime As Long
    dwHighDateTime As Long
End Type

Private Const WAIT_ABANDONED& = &H80&
Private Const WAIT_ABANDONED_0& = &H80&
Private Const WAIT_FAILED& = -1&
Private Const WAIT_IO_COMPLETION& = &HC0&
Private Const WAIT_OBJECT_0& = 0
Private Const WAIT_OBJECT_1& = 1
Private Const WAIT_TIMEOUT& = &H102&

Private Const INFINITE = &HFFFF
Private Const ERROR_ALREADY_EXISTS = 183&

Private Const QS_HOTKEY& = &H80
Private Const QS_KEY& = &H1
Private Const QS_MOUSEBUTTON& = &H4
Private Const QS_MOUSEMOVE& = &H2
Private Const QS_PAINT& = &H20
Private Const QS_POSTMESSAGE& = &H8
Private Const QS_SENDMESSAGE& = &H40
Private Const QS_TIMER& = &H10
Private Const QS_MOUSE& = (QS_MOUSEMOVE _
                            Or QS_MOUSEBUTTON)
Private Const QS_INPUT& = (QS_MOUSE _
                            Or QS_KEY)
Private Const QS_ALLEVENTS& = (QS_INPUT _
                            Or QS_POSTMESSAGE _
                            Or QS_TIMER _
                            Or QS_PAINT _
                            Or QS_HOTKEY)
Private Const QS_ALLINPUT& = (QS_SENDMESSAGE _
                            Or QS_PAINT _
                            Or QS_TIMER _
                            Or QS_POSTMESSAGE _
                            Or QS_MOUSEBUTTON _
                            Or QS_MOUSEMOVE _
                            Or QS_HOTKEY _
                            Or QS_KEY)

Private Declare Function CreateWaitableTimer Lib "kernel32" _
    Alias "CreateWaitableTimerA" ( _
    ByVal lpSemaphoreAttributes As Long, _
    ByVal bManualReset As Long, _
    ByVal lpName As String) As Long
   
Private Declare Function OpenWaitableTimer Lib "kernel32" _
    Alias "OpenWaitableTimerA" ( _
    ByVal dwDesiredAccess As Long, _
    ByVal bInheritHandle As Long, _
    ByVal lpName As String) As Long
   
Private Declare Function SetWaitableTimer Lib "kernel32" ( _
    ByVal hTimer As Long, _
    lpDueTime As FILETIME, _
    ByVal lPeriod As Long, _
    ByVal pfnCompletionRoutine As Long, _
    ByVal lpArgToCompletionRoutine As Long, _
    ByVal fResume As Long) As Long
   
Private Declare Function CancelWaitableTimer Lib "kernel32" ( _
    ByVal hTimer As Long)
   
Private Declare Function CloseHandle Lib "kernel32" ( _
    ByVal hObject As Long) As Long
   
Private Declare Function WaitForSingleObject Lib "kernel32" ( _
    ByVal hHandle As Long, _
    ByVal dwMilliseconds As Long) As Long
   
Private Declare Function MsgWaitForMultipleObjects Lib "user32" ( _
    ByVal nCount As Long, _
    pHandles As Long, _
    ByVal fWaitAll As Long, _
    ByVal dwMilliseconds As Long, _
    ByVal dwWakeMask As Long) As Long
   
Public Sub Wait(lNumberOfSeconds As Long)
    Dim ft As FILETIME
    Dim lBusy As Long
    Dim lRet As Long
    Dim dblDelay As Double
    Dim dblDelayLow As Double
    Dim dblUnits As Double
    Dim hTimer As Long
   
    hTimer = CreateWaitableTimer(0, True, App.EXEName & "Timer")
   
    If Err.LastDllError = ERROR_ALREADY_EXISTS Then
        ' If the timer already exists, it does not hurt to open it
        ' as long as the person who is trying to open it has the
        ' proper access rights.
    Else
        ft.dwLowDateTime = -1
        ft.dwHighDateTime = -1
        lRet = SetWaitableTimer(hTimer, ft, 0, 0, 0, 0)
    End If
   
    ' Convert the Units to nanoseconds.
    dblUnits = CDbl(&H10000) * CDbl(&H10000)
    dblDelay = CDbl(lNumberOfSeconds) * 1000 * 10000
   
    ' By setting the high/low time to a negative number, it tells
    ' the Wait (in SetWaitableTimer) to use an offset time as
    ' opposed to a hardcoded time. If it were positive, it would
    ' try to convert the value to GMT.
    ft.dwHighDateTime = -CLng(dblDelay / dblUnits) - 1
    dblDelayLow = -dblUnits * (dblDelay / dblUnits - _
        Fix(dblDelay / dblUnits))
   
    If dblDelayLow < CDbl(&H80000000) Then
        ' &H80000000 is MAX_LONG, so you are just making sure
        ' that you don't overflow when you try to stick it into
        ' the FILETIME structure.
        dblDelayLow = dblUnits + dblDelayLow
    End If
   
    ft.dwLowDateTime = CLng(dblDelayLow)
    lRet = SetWaitableTimer(hTimer, ft, 0, 0, 0, False)
   
    Do
        ' QS_ALLINPUT means that MsgWaitForMultipleObjects will
        ' return every time the thread in which it is running gets
        ' a message. If you wanted to handle messages in here you could,
        ' but by calling Doevents you are letting DefWindowProc
        ' do its normal windows message handling---Like DDE, etc.
        lBusy = MsgWaitForMultipleObjects(1, hTimer, False, _
            INFINITE, QS_ALLINPUT&)
        DoEvents
    Loop Until lBusy = WAIT_OBJECT_0
   
    ' Close the handles when you are done with them.
    CloseHandle hTimer

End Sub
'------------------------------------- End Paste

And here's an example:
106     Form1.txtSeed1.Text = Seed1Text
108     Form1.txtSeed1.Tag = Seed1Tag
         Wait 1
110     Form1.txtSeed5.Text = Seed5Text
112     Form1.txtSeed5.Tag = Seed5Tag
        Wait 1
114     Form1.txtSeed13.Text = Seed13Text
116     Form1.txtSeed13.Tag = Seed13Tag
        Wait 1
118     Form1.txtSeed21.Text = Seed21Text
120     Form1.txtSeed21.Tag = Seed21Tag
        Wait 1
122     Form1.txtSeed25.Text = Seed25Text
124     Form1.txtSeed25.Tag = Seed25Tag
0
 
jkaiosCommented:
Try calling the DoEvents function immediately after updating each textbox and before the Sleep API call.


Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

'Your Code
106     Form1.txtSeed1.Text = Seed1Text
108     Form1.txtSeed1.Tag = Seed1Tag
           DoEvents                                                  '<-- ADD THIS LINE
           Sleep(1000)
110     Form1.txtSeed5.Text = Seed5Text
112     Form1.txtSeed5.Tag = Seed5Tag
           DoEvents                                                  '<-- ADD THIS LINE
           Sleep(1000)
114     Form1.txtSeed13.Text = Seed13Text
116     Form1.txtSeed13.Tag = Seed13Tag
           DoEvents                                                  '<-- ADD THIS LINE
           Sleep(1000)
118     Form1.txtSeed21.Text = Seed21Text
120     Form1.txtSeed21.Tag = Seed21Tag
           DoEvents                                                  '<-- ADD THIS LINE
           Sleep(1000)
122     Form1.txtSeed25.Text = Seed25Text
124     Form1.txtSeed25.Tag = Seed25Tag
0
 
EDDYKTCommented:
I think the easy way is to use timer control

add timer control in your form

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

Move your update code back to your form
Private Sub Timer1_Timer()
    Static Count As Integer
   
    If Count = 0 Then
        Form1.txtSeed1.Text = Seed1Text
        Form1.txtSeed1.Tag = Seed1Tag
    ElseIf Count = 1 Then
        Form1.txtSeed5.Text = Seed5Text
        Form1.txtSeed5.Tag = Seed5Tag
    ElseIf Count = 2 Then
        Form1.txtSeed13.Text = Seed13Text
        Form1.txtSeed13.Tag = Seed13Tag
    ElseIf Count = 3 Then
        Form1.txtSeed21.Text = Seed21Text
        Form1.txtSeed21.Tag = Seed21Tag
    ElseIf Count = 4 Then
        Form1.txtSeed25.Text = Seed25Text
        Form1.txtSeed25.Tag = Seed25Tag
    Else
        Timer1.Enabled = False
    End If
    Count = Count + 1
End Sub
0
 
hesCommented:
Change
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

TO
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
In the Declaration section of the module
0
 
Shanmuga SundaramCommented:
or else try doing like this.

get the time and store in a variable.
create a loop
check for the time inside the loop. ensure doevents is included.
then check for the valid time - say 1 minute.
if it satisifies your condition then exit the loop.

I dont know how far this will help
0
 
Computer101Commented:
Forced accept.

Computer101
EE Admin
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
  • 2
  • +5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now