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

B

brunft

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
 
B

Brian Pelton

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
 
J

Jacky Kwok

Brian said:
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


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.



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

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
 

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

Similar Threads


Top