VB.NET How to Pause, Wait or Timer an Application for X seconds

I need to be able to pause execution for 5 seconds, how-to
mkisowAsked:
Who is Participating?
 
RBertoraConnect With a Mentor Commented:
 Private Declare Sub Sleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long)



  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Sleep(3000)
    MsgBox("hello")
  End Sub

Regards,
Rob Bertora
0
 
CuSo4Connect With a Mentor Commented:
Or you can just use this command:

Thread.Sleep(5000)
0
 
CuSo4Commented:
Oeps, I should heve add it, if you use Thread.Sleep(5000), you need to add the following statement before the rest of your code:

Imports System.Threading
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
Fahad MukhtarConnect With a Mentor Distinguished EngineerCommented:

Also, it will be better to make the user aware that the application is paused.. to acheive this you can set the Cursor to an Hourglass. and then reset the default cursor after pausing your application... below is some code for this

' CODE BEFORE PAUSE.....
'............
Cursor = Cursors.WaitCursor     ' Show an hourglass Cursor
System.Threading.Thread.Sleep(5000) ' Wait for 5 seconds
Cursor = Cursors.Default ' restore normal cursor
'............
' CODE AFTER PAUSE.....
'
0
 
KeirGordonConnect With a Mentor Commented:
You might do something more along the lines of

private sub Wait(howlong as integer)
 Cursor = Cursors.WaitCursor
 for x as integer = 0 to howlong*10
  System.Threading.Thread.Sleep(100)
  Application.DoEvents()
 next
 Cursor = Cursors.Default
end sub

.... I think if you just did a 5 second sleep your application would be completely unresponsive.... but with the DoEvents in the above version things will run a bit more smoothly.

I could be wrong ;)

--KG
0
 
KeirGordonCommented:
Keep in mind the DoEvents takes a 'little' time.... so your 5 seconds might run more like 5.0000001 seconds.... if that matters.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.