Timer Help

  • Thread starter Thread starter vinay
  • Start date Start date
V

vinay

Hi,

I need to use a Timer at run time. I have created a Timer
class which has method 'CreateTimer'.

I am able to create a timer and run the associated event
(code snippet follows), but I am unable to:

1. Stop this timer.
2. Give it a specific Name (sTimer), so that I can
control its interval dynamically.

Can you Please point out what I am missing in my logic ?


Public Function CreateTimer(ByVal sTimer As string)

Dim TB As Timer

Try

TB = New Timer

TB.Enabled = True
TB.Interval = 3000 '3 seconds delay
TB.Start()
AddHandler TB.Tick, AddressOf TimerEvents01

....

End Function

Private Sub TimerEvents01(ByVal sender As Object, ByVal e
As EventArgs)

Msgbox "Testing" & Now
End Sub


Regards

Vinay
 
vinay said:
Hi,

I need to use a Timer at run time. I have created a Timer
class which has method 'CreateTimer'.

I am able to create a timer and run the associated event
(code snippet follows), but I am unable to:

1. Stop this timer.

Call the Stop method or set Enabled = False
2. Give it a specific Name (sTimer), so that I can
control its interval dynamically.

Can you Please point out what I am missing in my logic ?


Public Function CreateTimer(ByVal sTimer As string)


What do you expect the function to return?

Dim TB As Timer

Try

TB = New Timer

TB.Enabled = True
TB.Interval = 3000 '3 seconds delay
TB.Start()
AddHandler TB.Tick, AddressOf TimerEvents01

....

End Function

Private Sub TimerEvents01(ByVal sender As Object, ByVal e
As EventArgs)

Msgbox "Testing" & Now
End Sub


Don't know if it helps:

Private m_Timer As Timer

Sub CreateTimer()

m_Timer = New Timer

m_Timer.Interval = 3000 '3 seconds delay
m_Timer.Start()
AddHandler m_Timer.Tick, AddressOf TimerEvents01

End Sub

Sub KillTimer
RemoveHandler m_Timer.Tick, AddressOf TimerEvents01
m_Timer.Stop
End Sub


Maybe you also want to add a Name property:

Public ReadOnly Name As String

Public Sub New(ByVal Name As String)
Me.Name = Name
End Sub
 
1. Stop this timer.

Why can you set the timer's Enabled property to False?
2. Give it a specific Name (sTimer), so that I can
control its interval dynamically.
Public Function CreateTimer(ByVal sTimer As string)

It looks like you have forgotten to set your return type. If you want to
return the timer from the code:

Public Function CreateTimer(ByVal sTimer As string) As Timer
Dim TB As Timer

Try

TB = New Timer

TB.Enabled = True
TB.Interval = 3000 '3 seconds delay
TB.Start()
AddHandler TB.Tick, AddressOf TimerEvents01

I would recommend setting the handler BEFORE enabling the timer.
....

End Function

Private Sub TimerEvents01(ByVal sender As Object, ByVal e
As EventArgs)

Msgbox "Testing" & Now
End Sub

Make sure you have Option Strict On

Chris
 
Hi Vinay,
Take a look at Timer which Timer you are using, there are at least 3 and
they are all very different
Microsoft.VisualBasic.Timer
Windows.Forms.Timer
System.Threading.Timer
I thought that the last one was almost not to stop because he has his own
thread
(I am not sure of that)
Cor
 
Chris Dunaway said:
I would recommend setting the handler BEFORE enabling the timer.

I wanted to suggest the same but as it is a System.Windows.Forms.Timer, the
first tick event does not occur before the app is idle, so it's not really
required to add the handler before.
 
Back
Top