开发者

How to suppress/catch System.ObjectDisposedException?

开发者 https://www.devze.com 2022-12-09 00:57 出处:网络
I have an application that sporadically throws this exception: System.ObjectDisposedException: Cannot access a disposed object.

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.

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...


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. So you basically have two options here: 1) Don't expose until you're finished using it. 2) Don't use it after it has been disposed.

EDIT: To help you troubleshoot the problem, you could set up ADPlus to create dump files for that specific exception. That could give you some insight as to why this happens. John Robbins has a Bugslayer article on how to do that. Please see http://msdn.microsoft.com/en-us/magazine/cc163530.aspx.


"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.


About catching the exception; I'm guessing there is no global exception handling in that application yet?

In your 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消