Reece Tilley Reece Tilley - 2 months ago 8
Vb.net Question

Delaying in VB.net

my issue is 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)


I think i'm doing something really simple wrong, i'm not the best at VB as i'm just learning so any explaining would be great!

~Thanks

Answer

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