Skip to content

Application.DispatcherUnhandledException Event

March 13, 2007

Tonight I was trying out RikReader (a WPF rss feed reader) and enjoying the experience…love those columns that flowdocument provides on my 20" monitor!

But then I came to a blog hosted on spaces.live.com.  For some reason, one of their image files makes WPF unhappy.  We are investigating the issue, but until then, developers should be aware of how to handle these problems.

 

Saturday, Douglas Stockwell (author of RikReader) posted the issue on this post.

At first, my colleague said to use the Image.ImageFailed event…however the image we found didn’t raise that event.

 

I’ve found that one way to solve this is by listening to:

Application.DispatcherUnhandledException Event

and doing something like this:

 

    public partial class App : System.Windows.Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            this.DispatcherUnhandledException += new System.Windows.Threading.DispatcherUnhandledExceptionEventHandler(App_DispatcherUnhandledException);
            base.OnStartup(e);
        }

        void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            //likely more elegant ways to do this...
            if (e.Exception.Message == "Cannot read from the stream.")
            {
                MainWindow.Background = System.Windows.Media.Brushes.Pink;
                e.Handled = true;
            }
        }
    }

[WOW, I just used Paste From Visual Studio tool in Windows Live Writer...Very nice!]

From → WPF

2 Comments
  1. Michael permalink

    This helps a lot. We couldn\’t figure for the life of us why an exception raised in our WPF Interop controls weren\’t getting caught by our (Winforms-based) UnhandledExceptionEventHandler. I guess I should have realized that WPF provided a counterpart to that. But it seems like a quirk because it IS still being hosted within a windows control.

  2. Tanveer permalink

    Hi Rob, I don\’t understand this line of yours:
    "[WOW, I just used Paste From Visual Studio tool in Windows Live Writer...Very nice!]".
     
    I tried to find the said feature in Live Writer itself, but it isn\’t available there. Do I have to install some other tools from ideas.live.com? I couldn\’t find anything on ideas.live.com either.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: