By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,033 Members | 1,285 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,033 IT Pros & Developers. It's quick & easy.

Timer Help

P: n/a
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
Nov 20 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"vinay" <cu****@cursor.com> wrote in news:0b9401c38109$c2b9c870
$a*******@phx.gbl:
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
Nov 20 '05 #2

P: n/a
"vinay" <cu****@cursor.com> schrieb
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
--
Armin

Nov 20 '05 #3

P: n/a
Cor
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
Nov 20 '05 #4

P: n/a
"Chris Dunaway" <du******@lunchmeatsbcglobal.net> schrieb
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.


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.
--
Armin

Nov 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.