1.11M Members

Pause VB 2012

 
0
 

How do you go about pausing your code from operating. I want to make an animation by making a picture box visable then another then another but I need a period of time between transitions? How would I go about doing this?

boredPic(1, 1).Visible = True
boredPic(1, 1).Image = My.Resources.Red
     'pause for half a second then
boredPic(1, 1).Visible = false
boredPic(2, 1).Visible = True
boredPic(2, 1).Image = My.Resources.Red
    'pause again for half a second
etc 
 
0
 

You could do something like this...

Dim stpWatchInfo As New System.Diagnostics.Stopwatch

boredPic(1, 1).Visible = True
boredPic(1, 1).Image = My.Resources.Red
     'pause for half a second then
stpWatchInfo.Restart()
while stpWatchInfo.Elapsed.TotalMilliseconds<500
'this is a 500 millisecond pause or 1/2 second

End While

boredPic(1, 1).Visible = false
boredPic(2, 1).Visible = True
boredPic(2, 1).Image = My.Resources.Red
    'pause again for half a second
stpWatchInfo.Restart()
while stpWatchInfo.Elapsed.TotalMilliseconds<500
    'this is a 500 millisecond pause or 1/2 second

End While
etc

That may work for you. let me know if you need more help or it doesn't work correctly.
Larry

 
1
 

You use a timer to track time and run the next thing when you want it to.
But... you don't want to "pause" your program or have the timer loop for a period, becuase if I am not mistaken that will freeze your main thread and no animation will show.
Use multi-threading or background worker for the timer and the track of time.
I'm sure you'll find lots of tutorials in here.

 
0
 

You can pause your application (the example here is for half a second) by

System.Threading.Thread.Sleep(500)

but what would be the point? All that you do is make your app unresponsive for the sleep time. VB programs are event driven. If you want some event to occur at set intervals then create a timer control and set the interval for the appropriate number of milliseconds. Put the code you want to execute in the timer Tick event.

 
0
 

Is there a way of playing the timer tick though in the middle of code being run? so the code will stop, run the timer(showing the animation or the picture box) then go back to the code and carry on till the end?

 
0
 

You have to break up your code into subs. Each sub can be executed when a timer expires. You can't pause your code and have it continue to execute at the same time.

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: