Solved

ObjectDisposedException

Posted on 2008-11-01
6
817 Views
Last Modified: 2013-11-17
Hi,

I use some custom controls on my CF2 Form I add dynamic and need to remove dynamic from my Form.
The matter is that I get an ObjectDisposedException when I dispose it at some of it's events.

So in the constructor I apply some events to the parent form eg.:
this.Parent.MouseUp += new MouseEventHandler(Parent_MouseUp);

When I dispose it I remove these events:
protected override void Dispose(bool disposing)
{
   this.Parent.MouseUp -= Parent_MouseUp;
}

However this don't solve the problem.
What works for me is to catch the ObjectDisposedException but I think this is not the cleanest way to make it working ...

Maybe somebody know a better solution?

Thanks,

Andre
0
Comment
Question by:andre72
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 22857473
you don't need that. Dispose() function is intended to free non-managed resources like GDI or database resources.
The events will be removed without your intervention when the object is no longer used.
0
 

Author Comment

by:andre72
ID: 22857484
Thanks for the information so you've also any idea about the Exception?
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 22857647
the exception is produced because the parent window was disposed at the moment the line of code is excecuted. But as I said, what you are doing is not needed.
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

Author Comment

by:andre72
ID: 22857681
Sorry for missunderstanding - it doesn't matter if I include it or not. Because of your hint I removed the lines as the dispose do it itself ...
But the problem is still the same ...
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 250 total points
ID: 22859907
remove all the dispose method.
0
 

Author Comment

by:andre72
ID: 22862266
The control get it's events from the parent control:
                this.Parent.MouseDown += new MouseEventHandler(Parent_MouseDown);
                this.Parent.MouseUp += new MouseEventHandler(Parent_MouseUp);
                this.Parent.MouseMove += new MouseEventHandler(Parent_MouseMove);

Isn't there a way to clear any event of it and / or clear the message queue?
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Code works but breaks when I add one section 4 30
VS2010 Build fails to install 14 82
C# XML Get Values 4 35
Visual C# Windows Project - Clone Form 4 26
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question