Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / December 2005

Tip: Looking for answers? Try searching our database.

Exceptions in System.Timers.Timer.Elapsed event are ignored & not reported - desired behaviour?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
brunft - 14 Dec 2005 11:30 GMT
I was testing this with .NET Framework 2.0.

Exceptions in the Elapsed event handler of a System.Timers.Timer are
ignored, they are not handled by the runtime and not reported (when
running outside of VS.NET) either. Ther Timer continues as if nothing
happened.
Is this the desired behaviour, because this would have very negative
impact on analysis of unexpected errors?
Am i missing any design directives concerining the exception handling
in a System.Timers.Timer object?

The following example demonstrates, what i am speaking of

using System;
using System.Timers;

class TimerTest
{
   Timer myTimer;

   public TimerTest() {
       myTimer = new Timer(1000);
       myTimer.Enabled = true;
       myTimer.Elapsed += new ElapsedEventHandler(myTimer_Elapsed);
   }

   void myTimer_Elapsed(object sender, ElapsedEventArgs e) {
       Console.WriteLine("TimerTest: Simulate an unexpected exception
...");
       // Simulate unexpected exception ...
       throw new Exception("Elapsed test exception.");
   }

}

class Program
{
   static void Main(string[] args) {
       TimerTest timerTest = new TimerTest();
       Console.WriteLine("TimerTest running. Press any key to
proceed");
       Console.Read();
   }

}

As already said, the exception is catched inside of VS.NET 2005 by the
Exception Assistant, when debugging.

Thanks & greets
brunft
Brian Pelton - 14 Dec 2005 19:34 GMT
I had this same issue and was dumbfounded for a while, and this is what
was told to me:

The event handler is running in a different thread.  So when an
exception occurs, the kills that thread, but the main app thread
continues to run.

--Brian

> I was testing this with .NET Framework 2.0.
>
[quoted text clipped - 47 lines]
> Thanks & greets
> brunft
Jacky Kwok - 15 Dec 2005 01:40 GMT
> I had this same issue and was dumbfounded for a while, and this is what
> was told to me:
[quoted text clipped - 4 lines]
>
> --Brian

Brain is right. The exception just can be handled in its own thread.
Hence, if main thread needs to handle the exception, the worker thread
should catch the exception and report an user defined event to main thread.

Another method is to handle "Application.ThreadException" and
"AppDomain.CurrentDomain.UnhandledException".

//--------------------
Application.ThreadException += new
ThreadExceptionEventHandler(ExceptionHandle);

AppDomain.CurrentDomain.UnhandledException += new
UnhandledExceptionEventHandler(DomainExceptionHandle);
//---------------------
They work in WinForm app but I do not sure about console app.

Signature

Jacky Kwok
jacky@alumni_DOT_cuhk_DOT_edu_DOT_hk
jacky@compose_DOT_com_DOT_hk

>> I was testing this with .NET Framework 2.0.
>>
[quoted text clipped - 47 lines]
>> Thanks & greets
>> brunft

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.