Timer not getting enabled after disabling?

  • Thread starter Thread starter Rich P
  • Start date Start date
R

Rich P

Greetings,

I have a routine I was running in VB6 on a timed schedule. When the
timeframe came up, the timer would be disable, the routine would run,
and the timer gets enabled. I am trying to migrate this project to
vb.net. But when the timer gets disable it is not getting re-enabled
after the routine is completed. I added Application.DoEvents just
before the call to Timer1.Enabled = True. But that did not help. Is
there something else I need to do to re-enable the timer?

Thanks,

Rich
 
Can you post the code that's doing the disabling and enabling? Also, are
you using a Timer control or one of the other timers?
 
Private Sub Timer1_Tick(...)
If boolStart = False Then
boolStart = True
runProc()
Timer1.Enabled = False
End If
End Sub

Private Sub Timer2_Tick(...)
If d1.TimeOfDay = "16:00:00" Then
Timer1.Enabled = True
End If
End Sub

runProc() runs a bunch of DTS Packages at a certain time. Right now I
added another boolean var to invoke runProc() and to turn it off.
Timer1 will enable if I don't have any procedures to run. But if I have
to invoke a procedure then Timer1 won't enable again. I got it to work
with the boolean var

Private Sub Timer1_Tick(...)
If boolStart = False Then
If boolRunProc = True Then
boolStart = True
runProc()
End If
End If
End Sub

This sort of works, but I think I would rather disable the timer and
then re-enable it. Maybe I need to use an event handler?
 
Rich,

I do not know if you can do this sample I made a short while ago, it is
about a splash form, however there are a lot of timers in it

Keep in mind that there are 4 timers by the way
windows.forms.timer
system.timers.timer
threading.thread.timer
and a Microsoft.VisualBasic one

\\\form1 needs 2 textboxes and a button
Private WithEvents frm As Form2
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim timer1 As New System.Windows.Forms.Timer
'You can drag it as well to your form, than those directcasts you
see are not needed
AddHandler timer1.Tick, AddressOf mytimer1
'this can as well be done using the 2 comboboxes on the codelayout
TextBox1.Text = "0"
timer1.Enabled = True
timer1.Interval = 400
Dim timer2 As New System.Windows.Forms.Timer
End Sub
Public Sub mytimer1(ByVal sender As Object, _
ByVal e As System.EventArgs) 'this is the event what is done when the
time elapsed
TextBox1.Text = (CInt(TextBox1.Text) + 1).ToString
DirectCast(sender, System.Windows.Forms.Timer).Enabled = True
'sender is the object of the timer
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
frm = New Form2
frm.Show()
frm.Top = Me.Top
frm.Left = Me.Left + 200
End Sub
Private Sub f_form2ready(ByVal message As String) _
Handles frm.form2ready
TextBox2.Text = message
frm.Dispose()
End Sub
///
\\\form2 needs one textbox
Friend Event form2ready(ByVal message As String)
Private Sub Form2_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim timer1 As New System.Windows.Forms.Timer
AddHandler timer1.Tick, AddressOf mytimer1
'this can as well be done using the 2 comboboxes on the codelayout
timer1.Enabled = True
timer1.Interval = 300
End Sub
Public Sub mytimer1(ByVal sender As Object, _
ByVal e As System.EventArgs)
Static counter As Integer = 0
Dim mystring As String = "I am searching for the time "
TextBox1.Text = mystring.Substring(counter)
DirectCast(sender, System.Windows.Forms.Timer).Enabled = True
'sender is the object of the timer
counter += 1
If counter = 22 Then
DirectCast(sender, Timer).Enabled = False
RaiseEvent form2ready(Now.TimeOfDay.ToString)
Else
Me.Opacity -= 0.04
DirectCast(sender, Timer).Enabled = True
'Sender is the object of the timer
End If
End Sub
///(I have this sample as well with a thread however that is not your
question

I hope this helps a little bit?

Cor
 
It turns out that I was just being a little too impatient. The timer
did come back on, but I had to wait a little bit longer. Thanks for
your suggestions, though.

Rich
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top