delay something for a few seconds?

defiance

Member
Joined
May 8, 2011
Messages
13
Programming Experience
Beginner
i remember in VB6 there is a command to delay something for a few seconds before moving on to the next step.
is there such a command for .net (i forgot what the command is for vb6)

an example of what i want to do below..

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
PictureBox1.Image = System.Drawing.Bitmap.FromFile("d:\warrior.bmp")
Catch ex As Exception
MsgBox("PLease insert the DVD to your DVD Drive.")
Finally
* Delay command insert here* <-- Want to put the delay here...
MsgBox("You have not inserted any DVD")
End Try
End Sub

thanz in advance..
 
Try using Sleep in Threading:


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
PictureBox1.Image = System.Drawing.Bitmap.FromFile("d:\warrior.bmp")
Catch ex As Exception
MsgBox("PLease insert the DVD to your DVD Drive.")
Finally
System.Threading.Thread.Sleep(6000)
MsgBox("You have not inserted any DVD")
End Try
End Sub


Hope this helps :D
 
hmm... thanz.. will look into that..

everything seems so radically different from vb6.
i have a lot of catching up to do
 
That's a very bad idea. If the user takes less than 6000 milliseconds to do the do then you leave them waiting around and if they take 6001 milliseconds then you tell them they didn't do what they actually did.
 
I just copied the code and replace "* Delay command insert here* <-- Want to put the delay here..." with "System.Threading.Thread.Sleep(6000)" . But its just now that I realized there is something wrong with the code presentation. My Bad. But anyways, System.Threading.Thread.Sleep(6000) will work in adding delays in your code. So just reconstruct the code and then you're good :D

 
Another option to the sleep command (because this can freeze the screen and does not allow for cancelling the sleep if needed) is the following.

VB.NET:
Dim tmCurTime as DateTime = Date.Now
Dim wait as Int = 6   ' Time in seconds
 
Do
  'Any code here
  Application.DoEvents()   ' This allows things to keep happening while the timer is running.
Loop Until DateDiff(DateInterval.Second, tmCurTime, Date.Now) = wait

With this code you can set KeyPreview to true and in the forms keydown event put something that if key = escape then set cancel variable to true, then in the do...loop routine above you can put in a statement that checks the cancel variable and if it is true then cancel out of the do or sub/function.
 
Back
Top