Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

I have written this code, thinking it should produce an effect of the labels "flickering" random numbers 25 times for.2 seconds each time.

 

However, all it seems to do is cause the labels to go blank for .2 seconds x 25 times before finally diaplying their final result.

 

code...

 

Dim i As Integer
           For i = 1 To 50
               Label1.Text = CStr(Int(Rnd() * 10))
               Label2.Text = CStr(Int(Rnd() * 10))
               Label3.Text = CStr(Int(Rnd() * 10))
               System.Threading.Thread.Sleep(200)
           Next i

 

What am I doing wrong?

 

TIA

Hammy

Edited by divil
  • *Experts*
Posted

Thread.Sleep puts the thread to sleep, but no Windows messages are processed, such as painting. To see the results of each loop, you'll need to refresh the form before you sleep.

 

You could do a simple Application.DoEvents() which will process all messages including paint. You could call Me.Refresh() to refresh the form but not handle other messages (such as mouse clicks). Or you could call each control's Refresh method (Label1.Refresh()) to refresh each control.

 

-ner

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
  • Moderators
Posted
The Application.DoEvents() that Divil suggested is the best way to go, but in VB6 I use Textboxes instead of labels (to stop the flickering) Though I haven't tested it out in .NET.
Visit...Bassic Software
  • *Experts*
Posted

DoEvents will definitely work but has one possible drawback (or benefit) - it allows windows to process events. If you have a long running subroutine and you need to update labels, you should use Refresh. If you were to use DoEvents, the user could click a button (such as a Save button) more than once or even close the form.

 

If you have a long running subroutine and you *want* the user to be able to click a button (such as Cancel) then you should use DoEvents.

 

-nerseus

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut

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...