tags:

views:

19

answers:

1

Just had an interesting problem, caused by the following sequence of events:

  • Code called from a WPF OnRender override threw an exception (there's no mystery in this - it's a trivial bug in some viewmodel code)

  • The exception was unhandled, so was picked-up by our DispatcherUnhandledException handler.

  • The DispatchedUnhandledException handler tries to open a WPF window to display the exception, allow reporting, etc. The window open fails, throwing another exception (from our global exception handler), which terminates the app.

It seems WPF takes violent exception (!) to an attempt to open a new window during an OnRender execution, and various bad things happen, ranging from a second Win32Exception being thrown by a CreateWindowEx deep within WPF, to the application terminating instantly if we try and do stuff like a MessageBox.Show in our exception handler when we detect the double exception.

I don't have any particular problem with dealing with this situation today, but there is a general question:

Is there any way to recover the WPF state from within an OnRender handler, so that ordinary WPF activities can continue? I tried catching the exception in OnRender and calling dc.Close() before rethrowing it, but that doesn't seem to make any difference. I suppose I should be looking for a 'WpfState.Reset()' call or something like it.

A: 

Is it possible that you tried to open the Window in another Thread than the main UI-thread? That would cause an exception. Try using the Dispatcher to open the window in the main thread if it isn't already.

As far as I recall - rendering takes place in a secondary thread to keep from blocking the UI-thread.

Goblin
Hmm. I'll have a look at that, though I didn't think OnRender is called on a different thread to the main U/I. I think this posting is perhaps closer to the mark though it doesn't offer any suggestions other than 'don't do that': http://blogs.msdn.com/b/nickkramer/archive/2006/05/06/591252.aspx
Will Dean