George,
It sounds like you really want the main thread to wait for the timer thread
to finish.
Rather then use a loop in the Main as every one suggested, I would recommend
you use an AutoResetEvent.
The timer routine will set the AutoResetEvent when the program is to end.
The Main routine will wait on the AutoResetEvent to be signaled.
This way the main routine will not consume ANY CPU time! (which is even less
time then using a loop & Thread.Sleep! ;-))
Something like:
Option Strict On
Option Explicit On
Module GeorgeGita
Private WithEvents timer As System.Timers.Timer
Private exitEvent As System.Threading.AutoResetEvent
Private padLock As New Object
Private count As Integer
Public Sub Main()
exitEvent = New System.Threading.AutoResetEvent(False)
timer = New System.Timers.Timer(5000)
timer.Enabled = True
exitEvent.WaitOne()
System.Threading.Thread.Sleep(1000)
End Sub
Private Sub Timer_Elapsed(ByVal sender As Object, ByVal e As
System.Timers.ElapsedEventArgs) Handles timer.Elapsed
SyncLock padLock
count += 1
Debug.WriteLine(count, "timer")
If count = 5 Then
exitEvent.Set()
End If
End SyncLock
End Sub
End Module
The exitEvent.WaitOne causes the Main routine to go to sleep indefinitely
until the event is Set. The Timer_Elapsed handler waits for the 5 elapsed
event, then set the exitEvent to signaled.
The SyncLock padLock prevents the Timer Elapsed event from being entered
multiple times, in case the amount of time to handle the Elapsed event is
longer than the duration on the timer, which generally is not a good idea
anyway ;-)
The Sleep after the exitEvent.WaitOne is to prevent 'skidding' ;-). I put it
there in case there are pending Timer_Elapse events, that have not finished
processing. There are better ways to handle it, which are dependant on what
the process is really doing.
Note: Do not confuse the AutoResetEvent event with normal VB.NET events such
as Timer.Elapsed. AutoResetEvent is an object used for multi-threaded
applications so one thread can signal a second thread that something
occurred, in this case the timer is signaling main that its ok for main to
exit. Where as Timer.Elapsed is used to let one object signal a second
object that a significant event occurred (if that explanation helped at all
;-))
Hope this helps
Jay