Problem trapping exceptions raised from Tick event

  • Thread starter Thread starter dempa
  • Start date Start date
D

dempa

Greetings!

I have this System.Windows.Forms.Timer which I create on the fly from
an instance method (let's call it MyMethod). It has a handler for the
Tick event that throws an exception, something like this:

throw new Exception("Timeout in yadda yadda...");

I place the call to MyMethod inside a try-catch block, something like
this:

try {
MyMethod();
} catch {
Trace.WriteLine("yaddayadda","yadda");
}

Now, when I run this using "Start Debugging (F5)" it works like a
charm. When I bypass debugging with "Start Without Debugging (Ctrl-
F5)" I get an "Unhandled exception" box with the usual Details,
Continue, Quit stuff. What am I doing wrong here?

I was using System.Timers.Timer at first but after reading some docs
it seemed like a bad idea. I read somewhere that
System.Windows.Forms.Timer would make sure that my exception got
passed on to my UI thread. And it looks like it works in debug mode...

I must be missing something fundamental here... Anyone?

Regards,
Dempa
 
Greetings!

I have this System.Windows.Forms.Timer which I create on the fly from
an instance method (let's call it MyMethod). It has a handler for the
Tick event that throws an exception, something like this:

throw new Exception("Timeout in yadda yadda...");

I place the call to MyMethod inside a try-catch block, something like
this:

try {
š MyMethod();} catch {

š Trace.WriteLine("yaddayadda","yadda");

}

Now, when I run this using "Start Debugging (F5)" it works like a
charm. When I bypass debugging with "Start Without Debugging (Ctrl-
F5)" I get an "Unhandled exception" box with the usual Details,
Continue, Quit stuff. What am I doing wrong here?

I was using System.Timers.Timer at first but after reading some docs
it seemed like a bad idea. I read somewhere that
System.Windows.Forms.Timer would make sure that my exception got
passed on to my UI thread. And it looks like it works in debug mode...

I must be missing something fundamental here... Anyone?

Regards,
Dempa

Hi Dempa,

Could you please provide a bit of actual code?
I think you should not throw exception from Tick handler because it is
called
from message loop processing routine. So there's no place to handle
the exception without
breaking message loop.

Thanks,
Sergey
 
Could you please provide a bit of actual code?
I think you should not throw exception from Tick handler because it is
called
from message loop processing routine. So there's no place to handle
the exception without
breaking message loop.

Well, I'm sure you're right. Throwing exceptions from such a handler
never seemed like a good idea. I have restructured the code a bit and
now I can live without that timeout, so for the time being - problem
solved (sort of).

Thanks for your time!

/dempa
 
Back
Top