Question return back to do more code after sub_delay(2000)

samir198811

New member
Joined
Aug 20, 2015
Messages
1
Programming Experience
5-10
Hi
I am new to this forum.
kindly I need help.

I have a vb.net form+button+textbox + delay sub

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''DELAY SUB'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Sub Delay(ByVal Milliseconds As Integer)
Dim Stop_Watch_2 As New Stopwatch
Stop_Watch_2.Start()
Do
Loop Until Stop_Watch_2.ElapsedMilliseconds >= Milliseconds
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For i = 1 To 10
Delay(2000) ' 2 second delay
Text_Box1.text = i.ToString
Button1.Left = Button1.Left + 30
Next
End Sub

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
when I run the form , I can see the button is moving to left OK every 2 seconds, the button moving on delay OK , BUT the text is not taking the loop value ?!!!
this delay method for me is better than thread.Sleep


so any suggestions how to let the text box update its content after the delay interval ?
 
Get rid of that "delay sub" and never do anything like that again. That is absolutely terrible code. It's called a "busy wait" and is one of the worst things you can ever do.

If there's something that you want to do at two second intervals then use a Timer. Set its Interval to 2000, call Start and handle its Tick event. When you're done, call Stop.
 
Back
Top