System.Timers.Timer in VB .NET ignores errors 
Author Message
 System.Timers.Timer in VB .NET ignores errors

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))
 End Sub

 Private Sub run()
     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
 End Sub

 Public Shared Sub Main()
     Dim foo as new TimerTest()
     foo.run()
 End Sub
    End Class
End NameSpace

************************************************



Sun, 03 Apr 2005 22:14:11 GMT  
 System.Timers.Timer in VB .NET ignores errors
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.


Quote:
> 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))
>  End Sub

>  Private Sub run()
>      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
>  End Sub

>  Public Shared Sub Main()
>      Dim foo as new TimerTest()
>      foo.run()
>  End Sub
>     End Class
> End NameSpace

> ************************************************



Mon, 04 Apr 2005 00:22:18 GMT  
 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

- Show quoted text -

Quote:
> > ************************************************



Mon, 04 Apr 2005 02:31:25 GMT  
 System.Timers.Timer in VB .NET ignores errors
Try this:

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))

Try

Throw New System.Exception("Throwing a system exception dammit!")

Catch ex As System.Exception

Console.WriteLine(ex.Message())

Finally

Console.WriteLine("This is printed after the exception is thrown")

End Try

End Sub

I believe the behaviour you exprience is by design. When you throw an
exception, the sub exits. Think of it like this - you call a sub with a
string which is a filename. You check the string for illegal filename chars,
find one, and throw an exception. If your sub keeps on running and attemps
to create the file, you have a problem.

The exception must be handled by the calling code, the client of your dll.
The example should not be used, it defeats the purpose.

Cheers
Paul


Quote:
> 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))
>  End Sub

>  Private Sub run()
>      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
>  End Sub

>  Public Shared Sub Main()
>      Dim foo as new TimerTest()
>      foo.run()
>  End Sub
>     End Class
> End NameSpace

> ************************************************



Mon, 04 Apr 2005 17:37:44 GMT  
 System.Timers.Timer in VB .NET ignores errors
Paul,

Thank you for the response, but I guess I wasn't clear enough in my original
message.  I am throwing the exception inside of the myTimer_Elapsed()
subroutine, and the subroutine is exiting, (which is what I want to happen),
but the calling code is not catching it at all.  Also, if I leave out all
error handling code, VB does not report an unhandled exception.

Cheers,
Zac

Quote:

> I believe the behaviour you exprience is by design. When you throw an
> exception, the sub exits. Think of it like this - you call a sub with a
> string which is a filename. You check the string for illegal filename
chars,
> find one, and throw an exception. If your sub keeps on running and attemps
> to create the file, you have a problem.

> The exception must be handled by the calling code, the client of your dll.
> The example should not be used, it defeats the purpose.

> Cheers
> Paul



> > 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))
> >  End Sub

> >  Private Sub run()
> >      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
> >  End Sub

> >  Public Shared Sub Main()
> >      Dim foo as new TimerTest()
> >      foo.run()
> >  End Sub
> >     End Class
> > End NameSpace

> > ************************************************



Mon, 04 Apr 2005 23:15:55 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. system.timers.timer bug found

2. Windows Service with System.Timers.Timer

3. Timer interval in VB.NET

4. VB.NET: Timer Running Many Times

5. Timer Stops firing when 2 VB.NET services running

6. Timer in vb .net

7. Server Timers vs. Windows Timers for RS232 polling

8. Timer property not working in VB.Net standard

9. problem with Timer Property of VB.NET

10. VB.net Timer

11. Benefits of the timer object, bompared to the timer control

12. better timer than Timer?

 

 
Powered by phpBB® Forum Software