Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sleep function Visual Basic

Tags:

sleep

vb.net

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.

like image 494
Jef Klak Avatar asked May 31 '11 18:05

Jef Klak


2 Answers

This one is much easie.

Threading.Thread.Sleep(3000)
like image 156
Helping Avatar answered Oct 02 '22 21:10

Helping


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.

like image 28
Brad Avatar answered Oct 02 '22 20:10

Brad