[vb.net] Sleep function Visual Basic

Is there a simple sleep function in Visual Basic that doens't involve thread.

Something similiar like there exists in: C: sleep(1);

We also tried this code:

Declare Sub Sleep Lib "kernel32" (ByVal milliseconds As Long)
' pause for 5 seconds
Sleep 5000

...but it didn't work. It gave me this error:

PInvokeStackImbalance was detected Message: A call to PInvoke function 'Ganzenbordspel!Ganzenbordspel.Spel::Sleep' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.

This question is related to vb.net sleep

The answer is


Since you are asking about .NET, you should change the parameter from Long to Integer. .NET's Integer is 32-bit. (Classic VB's integer was only 16-bit.)

Declare Sub Sleep Lib "kernel32.dll" (ByVal Milliseconds As Integer)

Really though, the managed method isn't difficult...

System.Threading.Thread.CurrentThread.Sleep(5000)

Be careful when you do this. In a forms application, you block the message pump and what not, making your program to appear to have hanged. Rarely is sleep a good idea.


This one is much easie.

Threading.Thread.Sleep(3000)