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.