system.timers.timer problems

B

Brent Burkart

I have a system.timers.timer that is not firing. Here is my code. Can
anyone see anything wrong?

Public Sub Main()

Dim aTimer As New System.Timers.Timer()

AddHandler aTimer.Elapsed, AddressOf OnTimedEvent

' Set the Interval to 5 seconds.

aTimer.Interval = 5000

aTimer.Enabled = True

End Sub

Private Sub OnTimedEvent(ByVal source As Object, ByVal e As
System.Timers.ElapsedEventArgs)

MsgBox("Hello!")

End Sub



Thanks,

Brent
 
B

Brent Burkart

Hmm... Still didn't work for me. I just step through the code and the
process ends when I am complete with Main. I added "a.Timer.Start()"
after "aTimer.enabled = true"
 
C

CJ Taylor

Hmm.. could be the fact your doing it in sub main...

You never start up an application message loop... Which means that the
addhandler is there, the start is a pretty way to invoke the timer on
another thread. could be its just dying before it ever reaches 5 seconds...

let me test..

yep... the application exists before the event can ever fire...

you have to use Application.Run to start a message loop which is how the
timer event receives its notification.

HTH,
CJ
 
S

Scott

If this is a console application then you probably don't have anything
happening to keep your program running. It simply runs the Sub Main
procedure and then exits before the 5 seconds elapses and the timer is
fired. It's not that the timer isn't functioning properly, it's simply that
your program exits before the OnTimedEvent function is called.
 
B

Brent Burkart

I haven't used application.run before.

Can you clue me in as to how to implement that.

I thought that application.run was to run an external application. I just
want to execute a procedure in my code.

I think I am confused.

Thanks for your help.
 
C

CJ Taylor

No,

Application.Run simple runs a .NET Form. (So the object must inherit from
System.Windows.Forms.Form) within an ApplciationContext in its own message
loop.

System.Diagnostic.Process.Start runs an external application, as does
Shell()

In order to execute your code, you must start a message loop... which if you
read the docs on application.run you can do that.

-CJ
 
B

Brent Burkart

I don't think this is going to work for me because I am not using any forms
in this application. I would make it a windows service, but I need to
implement this on Windows 98 machines.

Any other suggestions?

Thanks for your help
 

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