473,396 Members | 2,002 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Dispose called twice

Hi.
I put a message box in the form designer, Dispose method.
Clicking the form close (X) button, i noticed that the message,
appears twice.
My code does not call Dispose.
What is happening here?
Thanks.
Sharon.
Nov 16 '05 #1
4 5923
Hi Sharon,

You can add a boolean disposed member to your Form, initialized to false
(default). In Dispose, wrap your code in an if statement that checks
disposed to make sure it is false before doing anything. Then set disposed
to true.

Joe
--
http://www.csharp-station.com

"Sharon" <ta*******@void.null> wrote in message
news:OV**************@TK2MSFTNGP10.phx.gbl...
Hi.
I put a message box in the form designer, Dispose method.
Clicking the form close (X) button, i noticed that the message,
appears twice.
My code does not call Dispose.
What is happening here?
Thanks.
Sharon.

Nov 16 '05 #2
Hi Sharon,

I don't know how your Dispose is implemented, but if it is not
synchronized by using the lock(this) statement doing so would help
ensure that the method is not called more than once from different
threads. Of course if you don't have a flag indicating whether the
method has been called or not you would need to add it as well.

Yan

Nov 16 '05 #3
Telmo Sampaio <te***********@hotmail.com> wrote:
I do not know what is happening, however I can tell you you are not followig
the Dispose pattern correctly. The Dispose design pattern says that the
Dispose method can only be called once.


On the contrary - the Dispose design pattern specifically says that
IDisposable implementations should cope with Dispose being called
multiple times:

<quote>
If an object's Dispose method is called more than once, the object must
ignore all calls after the first one. The object must not throw an
exception if its Dispose method is called multiple times.
</quote>

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
Thanks for your help.
I'll add the boolean field to my form.
And then i need to find out why is Dispose() called twice.
Sharon.

"Sharon" <ta*******@void.null> wrote in message
news:OV**************@TK2MSFTNGP10.phx.gbl...
Hi.
I put a message box in the form designer, Dispose method.
Clicking the form close (X) button, i noticed that the message,
appears twice.
My code does not call Dispose.
What is happening here?
Thanks.
Sharon.

Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: faktujaa | last post by:
Hi All, A small confusion. I have defined a connection class that has System.Data.IDbConnection as a member variable and implements IDisposable interface. I have implemented Dispose method to call...
4
by: RiteshDotNet | last post by:
..net Frame work 1. Dispose Method what it does ? A. who its call / when it calls ? B. Is it fire automatically ? c. When dispose method is call what it does ? D. Release a Object from memory or...
11
by: Ken Durden | last post by:
I am in search of a comprehensive methodology of using these two object cleanup approaches to get rid of a number of bugs, unpleasantries, and cleanup-ordering issues we currently have in our...
15
by: Sam Sungshik Kong | last post by:
Hello! A disposable object's Dispose() method can be called either explicitly by the programmer or implicitly during finalization. If you call Dispose, the unmanaged resources are released...
16
by: Daniel Mori | last post by:
If an object implements the IDisposable interface (regardless if its a framework object or a user object), should I always dispose of that object out of principle?
3
by: Maxim | last post by:
Hi! According to documenation, if we need to release some umanaged resources manually, we need to implement IDisposable interface with single Dispose method. I am just wondering, what will...
2
by: Sharon | last post by:
Hi. I put a message box in the form designer, Dispose method. Clicking the form close (X) button, i noticed that the message, appears twice. My code does not call Dispose. What is happening...
2
by: KSC | last post by:
Hello, I have used a thread timer as in the documentation on MSDN in my VB.NET application. Using System.Threading.Interlocked.Increment I increment the counter to a certain point, perform an...
156
by: Dennis | last post by:
Ok, I'm trying to dispose of every object that I create that has a dispose method based on advice from this newsgroup. However, I'm not sure how to dispose of the following object that was created...
71
by: active | last post by:
In the main program I check to see if a certain form has been disposed. Does it make sense in that form's FormClosed event to do: Me.Dispose to make sure it is disposed the next time I check. Or...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.