0

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 the left every two seconds. The button moving on delay is OK, but the text is not taking the loop value. This delay method for me is better than thread.Sleep.

Any suggestions on how to let the text box update its content after the delay interval?

4
  • Thread.Sleep() ?? Maybe
    – Malcor
    Aug 20, 2015 at 16:14
  • @Malcor OP was mum on details, but he did say "this delay method for me is better than thread.Sleep.
    – TylerH
    Aug 20, 2015 at 16:18
  • @TylerH Yeah I caught that after I posted it, its been a long day.
    – Malcor
    Aug 20, 2015 at 16:19
  • 1
    the TextBox is "taking" it, it likely not repainting every update. Add Text_Box1.Refresh() Aug 20, 2015 at 16:23

1 Answer 1

2

This is running synchronously on your UI thread, blocking it from updating the UI until everything is all finished. You'll need to either call Text_Box1.Refresh, or yield control back to the message pump - so the UI can be naturally repainted - with Application.DoEvents.

Side note: You're busywaiting and probably shouldn't be. Even if you like this method of delaying better (for timing reasons or otherwise), consider adding a call to Thread.Sleep with a small value in your loop to give your user's CPU a break - or at least let it get back to encoding that cat video.

1
  • Thanks a lot, frankly I solved the problem by adding the 'do more code, which is after the timer finish point, I added it inside the timer itself
    – Nadir
    Aug 24, 2015 at 10:20

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.