We help IT Professionals succeed at work.

Pause in VB for random duration

DaveNoviceVB
DaveNoviceVB asked
on
Hello again,

help - pleeeaaasssse!
How do I pause an application in VB for a random number of seconds - is there a sleep function or something available?

What I want to do is display a picture - then pause a random time between 2-5 seconds - then display another picture.

If you can answer this I would be most greatful.
Thanks,
Dave from Scotland
Comment
Watch Question

hes
CERTIFIED EXPERT

Commented:
Use the sleep api

Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Public Const ConvToMilli = 60000 ' convert to milliseconds

to use for 2 seconds

Sleep(ConvToMilli * 2)
hes
CERTIFIED EXPERT

Commented:
For your random part

Dim MyPause
Randomize   ' Initialize random-number generator.

MyPause = Int((5 * Rnd) + 2)   ' Generate random value between 2 and 5.

Sleep(ConvToMilli * MyPause)

VK

Commented:
Hi, DaveNoviceVB !

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

Call Sleep(CLng(Int(3001 * Rnd) + 2000))

Commented:
Hello
I don't no if the Sleep win32 function freez the process. If no then the uper solution are good
if yes you must use a loop with a doevent instruction who re-give the hand to the system for other processus.

by example:
function sleep( iValue as integer)
dim first as variant
   first = now()
   do
      if (now()-first) > iValue then exit do
      doevent    
   while(true)

end function

best regard from france
Ryan ChongSoftware Tead Lead / Business Analyst / System Analyst / Data Engineer
CERTIFIED EXPERT

Commented:
Hi DaveNoviceVB,

You can try to use Timer Control also..

regards.
Commented:
Private waiting As Boolean

Private Sub Command1_Click()
    Randomize Timer
   
    Dim i As Integer
   
    Timer1.Interval = Int((5 - 2 + 1) * Rnd + 2)   'generate a random number between 2 and 5
    waiting = True
    Timer1.Enabled = True
    While waiting
        DoEvents
    Wend
   
    MsgBox "Waited " & Timer1.Interval & " seconds"
End Sub

Private Sub Form_Load()
    Timer1.Enabled = False
    waiting = False
End Sub

Private Sub Timer1_Timer()
    Timer1.Enabled = False
    waiting = False
End Sub
If this proposed answer has helped you, please accept it, grade it and close this question.  If it did not, reject it and add comments as to your progress and what else is needed.  If no action is taken here by next week, we'll need to decide this outcome.  Expert input always welcome as well.

Thanks,
Moondancer
Community Support Moderator @ Experts Exchange
Thank you for returning and finalizing this.  It is requested that anytime you choose a grade less than "A" you add a comment as to why.  If the "B" was a default posed to you, please let me know that as well, so I can refer to Engineering.

If this was in error, let me know that as well so I can change it to an A, since it does not impact the points you pay, but indeed impacts the Expert Points received, which are determined by the grade assigned.

Thanks,

Moondancer
Community Support Moderator @ Experts Exchange

Explore More ContentExplore courses, solutions, and other research materials related to this topic.