English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Using Visual basic. How do you program the timer so that it stops running the program for a couple of seconds, then starts back up from where it left off once the time has passed?

The reason i want to do this is for some "show" or "entertainment value". So far it only takes a second to get the end result I want, But i want to add a couple of images and some sounds and pretend that it is calculating. Then once the show is over, start back up from where it left off, and finish the program. So durring the time i pause the main stream part of the program, another sub or something is displaying a couple random numbers and making some sounds, and when the sub is done (or even when 3 or 4 seconds has passed) move on with the rest of the program. Any ideas?

Sorry if that was a bit repeatative but i gotta make sur eic over all the bases. Thanks!

2007-03-16 19:26:30 · 2 answers · asked by bath_tub_drain1 1 in Computers & Internet Programming & Design

2 answers

You can use a timer to create a pause in the execution of your code. Place a timer control on your forma and set its properties Enables = False Interval= 3000 (milliseconds)

Double click on the timer and create an event handler for the timer.Tick event . This event fires when the timer reaches the interval. Have this event reset the timer.enabled property to false

Now any time you want to delay add a Do While loop (you could make seperate delay subroutine) which will loop while the Timer is enabled. You Activate the timer just before entering this loop.

The key to making this work is to allow the program to handle other events while it is executing code. You do this in VB6 with the DoEvents method. (VBexpress puts this in My.Application.DoEvents . )

My example below ends up constantly checking the Doevents waiting for the timer to expire. Without this checking the code would be in an endless loop. You can use the cnt counter to slow down and reduce the number of doevent calls buy using an IF statement to call DoEvents every 1000 or so increments.

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim x As Integer
Dim cnt As Double

For x = 0 To 9
cnt = 0
Label1.Text = x
Label1.Invalidate()
Timer1.Enabled = True
Do
cnt += 1
My.Application.DoEvents()

Loop While Timer1.Enabled
Next
Label1.Text = cnt
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Timer1.Enabled = False
End Sub

2007-03-16 21:42:42 · answer #1 · answered by MarkG 7 · 0 0

if u r using .net u can pause the thread like this :

system.threading.thread.sleep(1000)

so your thread will sleep for 1 sec

2007-03-16 19:50:05 · answer #2 · answered by 14Me14U 2 · 0 0

fedest.com, questions and answers