sleep function

marclile

Newcomer
Joined
Mar 11, 2003
Messages
7
does anyone know how to make your app pause for a certain number of seconds?

i know that you can use System.Threading.Thread.Sleep() but that locks the app up. i was wanting be able to pause in the middle of my code without freezing the program.

any ideas? thanks.
 
well i use this way, not sure if it'll help you
Visual Basic:
'// at the top of your form.
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Const SLEEPTIME as Long = 100 ' what ever time you want in milliseconds goes here
'// then in the area you want to use the code, i'd use this...
Sleep SLEEPTIME
hope this helps.
 
That will just do the same as thread.sleep.

marclile, you can't have it both ways. Either you want your app to pause or not. You should investigate threading if you don't want your UI thread to lock up.
 
What are you hoping to accomplish via the pause?
There are some built-in pauses.
In the sendkeys class for example, there is a sendwait method that waits until its implementation has been accomplished before continuing.
Maybe some more detail might help us help you.

Jon
 
i was actually starting 2 different threads but i wanted to wait until the first thread completed before starting the second one. i was doing something like this:

t1.start()

do until t1.threadstate = stopped
thread.sleep
loop

t2.start()

but doing it like that makes the gui hang until t1 is finished.
 
Check into the Thread Class Join method.
The Join method suspends a thread's execution until another thread has finished its processing.

Here's an example....note the credit: I haven't personally used this in a program yet.

The program creates two threads and uses the thread class join method to suspend A's processing until thread B completes.

Visual Basic:
Imports System.Treading

Module Module1

Public A as Thread
Public B as Thread

Sub Display_A()
Dim I As Integer

B.Join()

For I = 0 to 250
Console.Write("A")
Next
End Sub

Sub Display_B()
Dim I As Integer
For I = 0 to 250
Console.Write("B")
Next
End Sub

Sub Main()
A = New Thread (AdressOf Display_A)
B = New Thread (AdressOf Display_B)

A.Start
B.Start

Console.ReadLine()  'Delay to view output

End Sub
End Module

credit to Jamsa: Visual Basic.Net Tips & Techniques

Jon
 
that's perfect. i'm pretty sure it's doing just what i want it to. here's an example of the code i'm using. you actually have to call the .join() after you've already started the thread.

Dim t1 As New System.Threading.Thread(AddressOf Function2)
t1.Start()
t1.Join()
Dim t2 As New System.Threading.Thread(AddressOf Function1)
t2.Start()

doing it this way, makes t2 wait until t1 has finished. i don't really know if i understand the Join() method because it says "Blocks the calling thread until a thread terminates." so you would think that in the above example, i would call the Join on t2 not t1.

oh well, this seems to work. thanks.
 
Back
Top