Click here to Skip to main content
15,888,039 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
Hello Codeproject i need some help how to make loop execute some code every 3 second?
and how to exit the loop? i need some suggestion i am still beginner in vb.net.
thank you.

this my code so far :

VB
Sub Loops(ByVal key As String)
'some code to make delay 3 second
'and then execute below code
  SendKeys.Send(key)
End Sub


so if i am call this code like this
VB
loop("B")

it will make automatic sendkeys B every 3 second.
and i need some code how to exit the loop?

thank you before.
Posted
Updated 9-Jun-13 12:51pm
v3

Most language variants have a sleep() or wait() type method. VB.Net does - Sleep(n) where n is the number of milliseconds - so 3000 milliseconds is equivalent to your 3 secs

you need to import System.Threading and use Sleep(3000) or use Thread.Sleep(3000)

I'd point out that the big issue with 'Sleep' as such is it blocks the thread (ie nothing else will happen, no UI updates etc) ..

'g'
 
Share this answer
 
v2
Comments
Gun Gun Febrianza 9-Jun-13 19:14pm    
oke sir i will try it now.. i will give a report soon .. vote 5 ;)
Gun Gun Febrianza 9-Jun-13 19:16pm    
but how to stop the loops?
Gun Gun Febrianza 9-Jun-13 19:20pm    
this is my code so far

Sub Loops(ByVal key As String)
For a = 1 To 10
Thread.Sleep(3000)
SendKeys.Send(key)
Next
End Sub
Private Sub RedemptionCheckBox124_CheckedChanged(ByVal sender As System.Object) Handles RedemptionCheckBox124.CheckedChanged
If RedemptionCheckBox124.Checked Then
Loops("A")
Else
'what i must do here to stop loops?
End If
End Sub
ok, I think Ive misunderstood your question on what you're trying to do ..as you asked the question, 'how to execute code every 3 seconds' within the original context of your 'Loops' method it was an ok start

as you have the modified code, in RedemptionCheckBox124, 'Loops' will be executed 10 times - this is a loop you set up .. every one of those 10 times its going to wait 3000 milli/3 seconds - you dont stop 'Loops' as such, it stops/control returns to RedemptionCheckBox124 after the entire 10 loops of 3 seconds ..... what I suspect you are trying to get to is something like a Background worker, but I think it's way above your experience level

http://www.dotnetperls.com/backgroundworker-vbnet[^]

...Im not sure if the standard VB.Net BackGroundWorker can be cancelled/terminated early though
 
Share this answer
 
Comments
Gun Gun Febrianza 11-Jun-13 4:32am    
ok i will try it soon , thank you sir. vote 5 for you , i think someone flamed here he was give me and you vote 1 start.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900