Click to See Complete Forum and Search --> : Timer Question


October 7th, 1999, 11:52 AM
Anyone know how to stop a timer?

Have tried Timer1.Enabled = False as it states at msdn.microsoft.com but this does NOT stop the timer.

Any help for this beginner would be appreciated.

czimmerman
October 7th, 1999, 11:58 AM
Timer1.enabled = false should work. Why don't you post your code so we can see what you are doing?

Charlie Zimmerman
http://www.freevbcode.com
czimmerman@freevbcode.com

October 7th, 1999, 12:04 PM
OK, here's the code:

Private Sub Form_Load()
'Center the Splash form on screen
frmSplash.Top = (Screen.Height - frmSplash.Height) / 2
frmSplash.Left = (Screen.Width - frmSplash.Width) / 2
'Show Splash Form
frmSplash.Show vbModeless
'Make sure Splash window stays on top
'grSetAsTopMost frmSplash, True
'Show progress bar status
Dim StartTime As Integer
StartTime = Second(Now)
prgBar.Visible = True
While time_elapsed < Timer1.Interval / 1000 And _
Timer1.Enabled = True
time_elapsed = (Second(Now) - StartTime)
prgBar.Value = time_elapsed
DoEvents
Wend
End Sub


Private Sub Frame1_Click()
'Unload Splash form if the user clicks on it
Timer1_Timer
End Sub


Private Sub Timer1_Timer()
'Unload Splash form when timer has expired (approx. 5 seconds)
Timer1.Enabled = False
Unload frmSplash
grSetAsTopMost frmMDIForm, True
frmMDIForm.Show
End Sub

czimmerman
October 7th, 1999, 01:49 PM
Your code has two errors:

1) Because time_elapsed as not been declared, it is an empty variant. You should either put Dim time_elapsed as integer and/or time_elapsed = 0 into your code (recommendation: put both).

2) The first line of the While Loop should read

While time_elapsed < Timer1.Interval / 1000 And _
Timer1.Enabled = true




Also, what you are trying to do can be accomplished more simply by using the Sleep API function. See http://www.freevbcode.com/ShowCode.Asp?ID=74 for an example.

October 7th, 1999, 02:04 PM
time_elapsed WAS declared in (General) as it is used elsewhere in my code.

the while loop DOES read Timer1.Enabled = True

czimmerman
October 7th, 1999, 03:55 PM
Then I don't know what to tell you. I got the following code to work just fine (it is the same logic with some of the other stuff commented out:

private Sub Form_Load()
'Center the Splash form on screen
Form2.Top = (Screen.Height - Form2.Height) / 2
Form2.Left = (Screen.Width - Form2.Width) / 2
'Show Splash Form
Form2.Show vbModeless
'Make sure Splash window stays on top
'grSetAsTopMost Form2, true
'Show progress bar status
Dim StartTime as Integer
StartTime = Second(Now)
time_elapsed = 0
' prgBar.Visible = true
While time_elapsed < Timer1.Interval / 1000 And _
Timer1.Enabled = true
time_elapsed = (Second(Now) - StartTime)
'prgBar.Value = time_elapsed
DoEvents
Wend
End Sub

private Sub Timer1_Timer()
'Unload Splash form when timer has expired (approx. 5 seconds)
Timer1.Enabled = false
Unload Form2
' grSetAsTopMost frmMDIForm, true
'frmMDIForm.Show
End Sub

BrewGuru99
October 7th, 1999, 06:01 PM
What do you mean "does NOT stop the timer"? When you unload the form, the timer is destroyed. What does it do after it's supposed to have been stopped, but doesn't?

Ravi Kiran
October 9th, 1999, 02:50 AM
Hi,
1.
What confuses me is Which Form's form_load is this code? czimmerman seems to think that this is the form load of a form which shows a different Splash form in its form load (which is what i too thought in the first look at the code) then what confused me was these lines:
grSetAsTopMost frmMDIForm, True
frmMDIForm.Show
When is this form (ie 'Me') is unloaded?
2. Dont put a while-wend loop in the Form_Load.
Form load is not finished till your timer is done, which could give trouble with DoEvents which will allow other events to be processed.
Do a simple modification to your code:
In Decl section:
dim timeofstart
private Sub Form_Load()
... other code
frmsplash.show VbModeless
....
timeofstart = timer() ' Timer() is another VB fn meant for this kind of stuff
timer1.interval = 500 ' Every .5 sec
timer1.Enabled = true
end sub
' Modify your timer event like this:
private Sub Timer1_Timer()
if timer() >= timeofstart + 5 then' after 5 secs stop
'Unload Splash form
Timer1.Enabled = false
Unload frmSplash
grSetAsTopMost frmMDIForm, true
frmMDIForm.Show
else
prgBar.Value = timer() - timeofstart
end if
End Sub




RK