在托管的Winforms控件中,在WPF应用程序中继续执行异常 [英] Continuing execution in WPF app after an exception in a hosted Winforms control
问题描述
NullReferenceException
。 虽然我已经设置一个 DispatcherUnhandledException
处理程序我无法捕获此处的异常并继续执行。
只有在 AppDomain.CurrentDomain.UnhandledException
处理程序我可以捕获它,但从那时起我不能做太多,应用程序只是退出。
然后我发现一个stackoverflow问题 (有一个答案,现在找不到)它表示尝试这样做:
System.Windows.Forms.Application.ThreadException + =(sender,args)=> ; {/ *抓住这里* /};
System.Windows.Forms.Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
没有帮助,因为(内联)处理程序没有被调用。 >
我正在错误的方式?
我不知道为什么这个处理程序在你的情况下永远不会被调用,可能是因为异常没有被抛出一个 Windows窗体线程(您的表单和控件的线程已创建),但通常设置处理程序 DispatcherUnhandledException
, AppDomain.UnhandledException
和/或 Application.ThreadException
不允许您阻止该过程的终止。他们是事件处理程序,而不是异常处理程序。即使您设置了这些事件处理程序,未处理的异常仍然是未处理的异常。通常,它们用于添加一些最终日志记录或向用户呈现有意义的消息。一旦提出了一个未处理的异常,您无法做任何事情来阻止该过程的终止。
I have a WPF app that has a WindowsFormsHost in which a 3rd party WinForms control is hosted. Sometimes, because of a bug in the 3rd party WinForms control I get a NullReferenceException
.
Although I had set up a DispatcherUnhandledException
handler I can't catch the exception there and continue the execution.
Only in the AppDomain.CurrentDomain.UnhandledException
handler I can "catch" it but I cannot do much from then on, the application simply exits.
Then I found a stackoverflow question (with an answer; can't find it now) which stated to try to do this :
System.Windows.Forms.Application.ThreadException += (sender, args) => { /* Catch it here */};
System.Windows.Forms.Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
That didn't help either because the (inline) handler did not get called ever.
Am I going the wrong way?
I am not sure why that handler never gets called in your case, probably because the exception was not thrown on a Windows Forms thread (the thread on which your forms and controls have been created), but generally, setting up handlers for DispatcherUnhandledException
, AppDomain.UnhandledException
and/or Application.ThreadException
doesn't allow you to prevent the termination of the process. They are event handlers, not exception handlers. The unhandled exception is still an unhandled exception even if you have setup those event handlers. Usually they are used to add some final logging or present a meaningful message to the user. Once an unhandled exception is raised there is nothing you can do to prevent the termination of the process.
这篇关于在托管的Winforms控件中,在WPF应用程序中继续执行异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!