How do I recover after problem in WPF OnRender

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.


    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.

    链接地址: http://www.djcxy.com/p/51228.html

    上一篇: WPF数据绑定中的异常处理

    下一篇: 如何在WPF OnRender出现问题后恢复