WPF Dispatcher.UnhandledException within a ShowDialog call.

Posted by Eric on Stack Overflow See other posts from Stack Overflow or by Eric
Published on 2010-04-12T23:30:49Z Indexed on 2010/04/12 23:32 UTC
Read the original article Hit count: 606

It appears that ShowDialog() invokes the Dispatcher message handling loop within. Thus, you have a stack that looks something like:

Outer-most Dispatcher message loop
...
x.ShowDialog()
Inner Dispatcher message loop
...

I am using the Dispatcher.UnhandledException to catch exceptions not handled by my code. However, it appears that the Inner Dispatcher message loop, above, is undesirably catching exceptions that my code would catch. Example:

Outer-most Dispatcher message loop
try/catch FooException
...
x.ShowDialog()
Inner Dispatcher message loop
...
throw FooException

What I would like is for the thrown FooException to get caught by the try/catch. However. It gets caught first by the (inner) Dispatcher.UnhandledException.

I see there are ways to filter the exception. However, those filters will apply to both the inner and outer most handlers. What I am looking for is to have my Dispatcher.UnhandledException code run only on the outer-most dispatcher message loop. Does that make sense?

I could, of course, reflect the call stack from within my handler to see if this is the outer-most dispatcher, but that seems a bit fragile. Other ideas?

Thanks!

Eric

© Stack Overflow or respective owner

Related posts about wpf

Related posts about dispatcher