简体   繁体   English

如何压缩/捕获System.ObjectDisposedException?

[英]How to suppress/catch System.ObjectDisposedException?

I have an application that sporadically throws this exception: 我有一个偶尔抛出此异常的应用程序:

System.ObjectDisposedException: Cannot access a disposed object.
Object name: "Panel". 
   bei System.Windows.Forms.Control.CreateHandle() 
   bei System.Windows.Forms.Control.get_Handle() 
   bei System.Windows.Forms.ContainerControl.FocusActiveControlInternal() 
   bei System.Windows.Forms.Form.set_Active(Boolean value) 
   bei System.Windows.Forms.Form.WmActivate(Message& m) 
   bei System.Windows.Forms.Form.WndProc(Message& m) 
   bei System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) 
   bei System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 
   bei System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

Is there a way to suppress this exception ideally without touching the code? 有没有办法在不触及代码的情况下理想地抑制此异常? I am thinking about some registry magic or esoteric .NET configs. 我正在考虑一些注册表魔术或深奥的.NET配置。

Furthermore I am of course also interested in ways to catch this exception. 此外,我当然也对捕获此异常的方法感兴趣。 There seems to be no hook for me to catch this exception... And of course it is not reproducible... 似乎没有钩子让我抓住这个例外...当然它不可重复......

"Is there a way to fix the pain in my left leg that I get every time I shoot myself in the foot? Perhaps taking copious amounts of paracetamol or similar?" “有没有办法解决我每次用脚射击自己的左腿疼痛?也许是服用了大量的扑热息痛或类似物?”

The answer here is not to fix the pain, it's avoiding shooting yourself in the foot. 这里的答案不是解决疼痛,而是避免自己在脚下射击。

In this case, there's code accessing a panel that is disposed. 在这种情况下,有代码访问处理的面板。 This code must be fixed, the answer is not to suppress the exception because you really do have a bug in your code. 此代码必须是固定的,答案是不抑制异常,因为你真的在你的代码中的错误。 It's not the runtime that is in fault here. 这不是故障的运行时。

Now, I see from comments that you want a "quick fix" to get back to work, that kind of attitude is not going to help you for long because, as you see, every answer and possible solution brings new questions. 现在,我从评论中看到你想要“快速修复”以恢复工作,这种态度不会长期帮助你,因为正如你所看到的,每个答案和可能的解决方案都会带来新的问题。

So instead of spending time trying to silence the runtime telling you that you have a bug, stop doing that and fix the bug. 因此,不要花时间试图让运行时静默,告诉你有一个bug,而是停止这样做并修复bug。

The runtime is trying to tell you something. 运行时试图告诉你一些事情。 Don't ignore it! 不要忽视它! Catching and ignoring the exception does not make the problem go away. 捕获并忽略异常并不会使问题消失。

The specific exception tells you that you're trying to use a Panel after it has been disposed. 特定的例外情况告诉您,在处理完Panel后,您正尝试使用它。 So you basically have two options here: 1) Don't expose until you're finished using it. 所以你基本上有两个选择:1)在你完成使用之前不要暴露。 2) Don't use it after it has been disposed. 2)处理后请勿使用。

EDIT: To help you troubleshoot the problem, you could set up ADPlus to create dump files for that specific exception. 编辑:为了帮助您解决问题,您可以设置ADPlus以创建该特定异常的转储文件。 That could give you some insight as to why this happens. 这可以让您了解为什么会发生这种情况。 John Robbins has a Bugslayer article on how to do that. John Robbins有一篇关于如何做到这一点的Bugslayer文章。 Please see http://msdn.microsoft.com/en-us/magazine/cc163530.aspx . 请参阅http://msdn.microsoft.com/en-us/magazine/cc163530.aspx

About catching the exception; 关于捕获异常; I'm guessing there is no global exception handling in that application yet? 我猜这个应用程序中没有全局异常处理了吗?

In your Program.cs: 在您的Program.cs中:

static void Main()
{
    Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
    AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

    Application.Run(new MainForm());
}

static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
    // Handle exception
    Application.Exit();
}

static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
    // Handle exception
    Application.Exit();
}

Adding this, and gracefully handling the exception would be a good thing for the user. 添加它,并优雅地处理异常对用户来说是一件好事。 If you add some additional logging, it might even help you eliminate the cause for the exception. 如果添加一些额外的日志记录,它甚至可以帮助您消除异常的原因。 Specifically targeting the ObjectDisposedException can be done by checking the exception type in the above handlers. 可以通过检查上述处理程序中的异常类型来专门针对ObjectDisposedException

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM