-1

I need to wait 3-4 seconds after a button has been pressed before i can check for it, here is my code under button1_click:

While Not File.Exists(LastCap)
    Application.DoEvents()
    MsgBox("testtestetstets")
End While

PictureBox1.Load(LastCap)
Michael M.
  • 10,486
  • 9
  • 18
  • 34
Reece Tilley
  • 17
  • 1
  • 1
  • 3
  • 4
    Why do you have to wait? – Steve Nov 22 '12 at 20:06
  • Why you dont use a `Timer`? with `Interval = 3000`ms. On tick stop Timer and do what you need – Epsil0neR Nov 22 '12 at 20:27
  • 5
    Does this answer your question? [How do I delay code execution in Visual Basic (VB6)?](https://stackoverflow.com/questions/95112/how-do-i-delay-code-execution-in-visual-basic-vb6) – Michael M. Jun 02 '23 at 14:19

5 Answers5

6

If the reason you are needing to wait is for the file to be created try using a FileSystemWatcher and respond to the Created and Changed Events that way you are responding to an event rather than arbitrarily waiting a select period of time.

Something like:

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    FileSystemWatcher1.Path = 'Your Path Here
    FileSystemWatcher1.EnableRaisingEvents = True
   'Do what you need to todo to initiate the file creation
End Sub

Private Sub FileSystemWatcher1_Created(sender As Object, e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Created, FileSystemWatcher1.Changed
    If e.Name = LastCap Then
        If (System.IO.File.Exists(e.FullPath)) Then
            FileSystemWatcher1.EnableRaisingEvents = False
            PictureBox1.Load(e.FullPath)
        End If
    End If
End Sub
Mark Hall
  • 53,938
  • 9
  • 94
  • 111
5

You can use, although not recommended:

Threading.Thread.Sleep(3000) 'ms

This will wait 3 seconds, but also block everything else on the same thread. If you run this in the form your user-interface will not response until the wait is over.

just as a side note: use MessageBox.Show("My message") instead of MsgBox (latter is from old VB).

5

If you want your form to continue to function while the 3 seconds pass, you can add a Timer control instead, with some code like this:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    ' set the timer
    Timer1.Interval = 3000 'ms
    Timer1.Start()
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    Timer1.Stop()
    'add delayed code here
    '...
    '...
    MessageBox.Show("Delayed message...")
End Sub

Drag and drop a Timer control from your toolbox to your form. It's not visible at runtime

Derek Tomes
  • 3,989
  • 3
  • 27
  • 41
5

or better yet making a wait function using stop watch, this wont halt the process in the same thread like thread sleep

 ' Loops for a specificied period of time (milliseconds)
Private Sub wait(ByVal interval As Integer)
    Dim sw As New Stopwatch
    sw.Start()
    Do While sw.ElapsedMilliseconds < interval
        ' Allows UI to remain responsive
        Application.DoEvents()
    Loop
    sw.Stop()
End Sub

usage

wait(3000)

for 3 sec delay

Joe Dabones
  • 91
  • 1
  • 1
3

You could use this

Public Sub BeLazy()
    For i = 1 To 30
        Threading.Thread.Sleep(100)
        Application.DoEvents()
    Next
End Sub

It will delay for 3 seconds.

HasanG
  • 12,734
  • 29
  • 100
  • 154
Joe Dabones
  • 91
  • 1
  • 1