Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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?

Posted
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.
"Who is John Galt?"
Posted
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.
  • Leaders
Posted

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

[sIGPIC]e[/sIGPIC]
Posted
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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...