
System.Timers.Timer in VB .NET ignores errors
I added the Throw line in two different places inside my code: one in the
myTimer_Elapsed() subroutine, and the other in the Catch block in the run()
subroutine. I also added a try/catch block around the call to the run()
method in Main() subroutine. I still can not see any exceptions being
thrown.
I also added a line of code to write "This is printed after the exception is
thrown" in the myTimer_Elapsed() subroutine after I throw the exception, and
it is not being printed, which tells me that the myTimer_Elapsed()
subroutine is bombing out after the exception is thrown. This leads me to
believe that the exception may be caught by something inside the timer and
discarded. Can anyone confirm or correct this theory?
I have pasted updated code at the bottom of this message.
Thanks!
-Zac
Quote:
> Try using the following in your catch block:
> Throw New System.Exception("Throwing a system exception damnit!")
> You will need to handle the system exception at the next higher level in
> your callstack.
> > Hi,
> > I am trying to write a routine in Visual Basic that will raise an error
> > after a certain amount of time has passed. I attempted to use a
> > System.Timers.Timer to do this, but the error is not being raised when
> then
> > timer elapsed event is raised. I know the function I have handling the
> > event is being executed. After wrestling with this for a while, I tried
> > using a System.Threading.Timer object. When I use this timer, the error
> is
> > being raised, but I can not catch it because the timer is executing in
> > another thread. I can't use a System.Windows.Forms.Timer because this
> > routine is going into a class library. I have included my
> > System.Timers.Timer code below. Any help or suggestions are greatly
> > appreciated.
> > Cheers,
> > Zac
> > ********* System.Timers.Timer Code ****************
Imports System
Imports Microsoft.VisualBasic
NameSpace TT
Public Class TimerTest
Private WithEvents myTimer as System.Timers.Timer
Private counter as Integer = 0
Private max_ticks=10
Private Sub myTimer_Elapsed(ByVal sender as Object, ByVal e As
System.Timers.ElapsedEventArgs) Handles myTimer.Elapsed
counter += 1
Console.WriteLine("Timer Elapsed "+CStr(counter))
Throw New System.Exception("Throwing a system exception dammit!")
Console.WriteLine("This is printed after the exception is thrown")
End Sub
Private Sub run()
Try
myTimer = New System.Timers.Timer()
myTimer.AutoReset=True
myTimer.Interval=100
myTimer.Start()
do while counter < max_ticks
if counter > max_ticks then exit sub
loop
Catch
Throw New System.Exception("Throw another system exception")
End Try
End Sub
Public Shared Sub Main()
Dim foo as new TimerTest()
Try
foo.run()
Catch
Console.WriteLine("Exception caught")
End Try
End Sub
End Class
End NameSpace
Quote:
> > ************************************************