Problem trapping exceptions raised from Tick event

Problem trapping exceptions raised from Tick event

am 24.01.2008 16:47:17 von 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

Re: Problem trapping exceptions raised from Tick event

am 24.01.2008 21:52:38 von forever.zet

On 24 ÑÎ=D7, 17:47, dempa wrote:
> 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 {
> =9A MyMethod();} catch {
>
> =9A 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

Re: Problem trapping exceptions raised from Tick event

am 25.01.2008 00:07:47 von 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.

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