Question "Start/Stop" Buttons on Form

mBrannam

New member
Joined
Mar 30, 2011
Messages
4
Programming Experience
1-3
I currently have a working program that creates one colored pixel in a Bitmap, displays the bitmap in a pictureBox, and "jiggles" the pixel by making it move in a random direction by 1 pixel every time a button is clicked.

I want to make the pixel move automatically until a Stop button is pressed.

I have tried

VB.NET:
While 1 <> 2
     Pixel.move() 'move the pixel in a random direction
     Me.outputBox.Image = bmp 'update the picturebox
     System.Threads.Thread.Sleep(100) 'wait
End While

for the code in the "Start" button, but the program just hangs indefinitely without updating the pictureBox at all.

Any suggestions?
 
The program freezes because you are running that loop on the UI thread. You are continually sleeping the UI thread with a tiny bit of work in between, so it never gets a chance to update the UI.

Don't use a loop at all. Add a Timer to your form and set its Interval to 100. Add a Tick event handler and move the PictureBox once and once only in that method. Now you can simply Start and Stop your Timer using your Button.
 
Back
Top