Solved

Programmed pause in VB6

Posted on 2007-11-15
14
769 Views
Last Modified: 2013-12-26
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
Comment
Question by:wjshore
  • 3
  • 3
  • 2
  • +5
14 Comments
 
LVL 8

Expert Comment

by:Wikkard
ID: 20293093
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
 
LVL 8

Expert Comment

by:Wikkard
ID: 20293115
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
 

Author Comment

by:wjshore
ID: 20293227
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
 
LVL 20

Expert Comment

by:hes
ID: 20293261
'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
 

Author Comment

by:wjshore
ID: 20293729
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
 
LVL 8

Expert Comment

by:Wikkard
ID: 20293894
The answer should be the same
:-)
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:wjshore
ID: 20293928
Nope.  With the Private Declare in the module's General Declarations section and the code listed as suggested, the program hangs.
0
 
LVL 11

Expert Comment

by:TreyH
ID: 20295491
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
 
LVL 12

Accepted Solution

by:
jkaios earned 250 total points
ID: 20295815
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
 
LVL 26

Assisted Solution

by:EDDYKT
EDDYKT earned 250 total points
ID: 20297848
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
 
LVL 20

Expert Comment

by:hes
ID: 20297898
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
 
LVL 17

Expert Comment

by:Shanmuga Sundaram
ID: 20320129
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
 
LVL 1

Expert Comment

by:Computer101
ID: 21000172
Forced accept.

Computer101
EE Admin
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now