HellVeN Posted June 18, 2005 Posted June 18, 2005 Here's a simplified version of a bit of code: Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click Dim counter As Integer Dim temp As String While counter < 11 temp = Button1.Text Button1.Text = Button2.Text Button2.Text = temp Threading.Thread.Sleep(1000) counter += 1 End While End Sub I want the program to stop for 1 second after each switch but what this piece of code does is stop 11 seconds then does the switch. I want it to stop and show each switch. How can I do that? Is it possible to do it without a timer? Quote
IngisKahn Posted June 18, 2005 Posted June 18, 2005 IMO a timer would be a much nicer solution. Sleeping a GUI thread is never a good way to go. Anyway, the problem is that your program can't paint itself while it's sleeping. DoEvents or a call to Paint would help a bit. Quote "Who is John Galt?"
jmcilhinney Posted June 19, 2005 Posted June 19, 2005 You can also explicitly call Me.Refresh() (or Button1.Refresh() and Button2.Refresh()) just before the call to Sleep() because basically your code doesn't allow the time for an implicit refresh. I agree with IngisKhan, though, that putting your main thread to sleep is a bad idea. Your app will be completely frozen for that time. Quote
Leaders snarfblam Posted June 19, 2005 Leaders Posted June 19, 2005 If you don't want to use a timer, you can also make your own pause loop. Sub Pause(ByVal Milliseconds As Integer) Dim EndTime As Long = Environment.TickCount + Milliseconds Do While EndTime > Environment.TickCount Application.DoEvents() Loop End Sub Quote [sIGPIC]e[/sIGPIC]
jmcilhinney Posted June 20, 2005 Posted June 20, 2005 If you don't want to use a timer, you can also make your own pause loop. Sub Pause(ByVal Milliseconds As Integer) Dim EndTime As Long = Environment.TickCount + Milliseconds Do While EndTime > Environment.TickCount Application.DoEvents() Loop End Sub I don't mean to be rude, but I would think that using a loop to create a delay would be frowned upon in polite circles. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.