destroy instances...


A

anthony

Hi, there is sth i am not sure, i looked at the help in vb.net for creating
timer on the fly (see below).

Since the event would happen 10sec later, I cannot destroy the timer right
away (or can I? or will VB.net destroy automatically?), I can do it after
the event is fired, but the timer is not in scope in the event subroutine.
If I leave it as is, if the application continue to fire the timer over
time, then it would continue to consume system resource?


Public Shared Sub Main()
' Create a new Timer with Interval set to 10 seconds.
Dim aTimer As New System.Windows.Forms.Timer(10000)
AddHandler aTimer.Elapsed, AddressOf OnTimedEvent
' Only raise the event the first time Interval elapses.
aTimer.AutoReset = False
aTimer.Enabled = True

Console.WriteLine("Press 'q' to quit the sample.")
While Console.Read() <> CInt("q")
End While
End Sub

' Specify what you want to happen when the event is raised.
Private Shared Sub OnTimedEvent(source As Object, e As ElapsedEventArgs)
Console.WriteLine("Hello World!")
End Sub
 
Ad

Advertisements

C

Chris, Master of All Things Insignificant

If you want the timer to stop "firing" just do a

Private Shared Sub OnTimedEvent(source As Object, e As ElapsedEventArgs)
dim aTimer as Timer
aTimer = DirectCast(Source, Timer)
aTimer.Enabled = False
End Sub

This will stop it from firing until it is enabled again.

Is this what you need?
Chris
 
C

Cor Ligthert

Anthony,

Mostly with the form timer is the first thing I do in the tick event is
setting the timer off
And because you did create it in your sub main it can be like this. Typed in
this message so watch typos.

\\\
directcast(sender,windows.forms.timer).enabled = false
///
Why did you use that word "source" where sender is standard?

I hope this helps?

Cor
 
Ad

Advertisements

H

Herfried K. Wagner [MVP]

anthony said:
Public Shared Sub Main()
' Create a new Timer with Interval set to 10 seconds.
Dim aTimer As New System.Windows.Forms.Timer(10000)

MSDN on 'System.Windows.Forms.Timer':
 

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

Top