473,402 Members | 2,064 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,402 software developers and data experts.

Question about Control.IsDisposed

Hello, folks!

I am trying to understand how IsDisposed property of Control class works.
I need to use the following code in my application.

if frm == null or frm.IsDisposed
...

MS explains about IsDisposed like the following.

<snip>
When this property returns true, the control is disposed of and can no
longer be referenced as a valid Windows control. Even though the instance of
a control is disposed of, it is still maintained in memory until it is
removed from memory through garbage collection.
</snip>

I don't understand several things regarding above explanation and
IsDisposed.

1. Is a control maintained in memory even after it's disposed?
2. If a control is garbage collected, how can I access the IsDisposed
property (property is part of control)?
3. Can I rely on the code (if frm == null or frm.IsDisposed) when I want to
check if the frm is created and accessible?

Thanks.

Sam
Nov 16 '05 #1
2 6592
Hi Sam,
1. Is a control maintained in memory even after it's disposed?
1. Yes, you can call Dispose() on a control and still have a reference to it. The Dispose method
"Disposes of the resources (other than memory)". It is used to speed up garbage collection and to free file handles and resources not cleaned up by the garbage collector.
2. If a control is garbage collected, how can I access the IsDisposed
property (property is part of control)?
In theory, you can't, since if you had a reference to the object you want to check IsDisposed of, then the garbage collector wouldn't have released it.
3. Can I rely on the code (if frm == null or frm.IsDisposed) when I want to
check if the frm is created and accessible?


Someone else would know more than me on this, but I would imagine

if(frm != null)
{
if(!frm.IsDisposed)
{
// form is still working
}
}

There might be more to it though, especially in multithreading.

--
Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #2
Morten Wennevik <Mo************@hotmail.com> wrote:
3. Can I rely on the code (if frm == null or frm.IsDisposed) when I want to
check if the frm is created and accessible?
Someone else would know more than me on this, but I would imagine

if(frm != null)
{
if(!frm.IsDisposed)
{
// form is still working
}
}


I don't think there's any benefit in breaking it up into two stages
here. Just use

if (frm!=null && !frm.IsDisposed)
{
....
}
There might be more to it though, especially in multi-threading.


If you need to know about it from a non-UI thread, you'd need to use
Control.Invoke - but by the time the delegate returned, the result
might be wrong!

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3

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

Similar topics

4
by: Steve Peterson | last post by:
Hello Just a quick question.. I have a MDI app that allows the user to open several child forms.. Nothing special. However, what I would like to know is how to prevent a user for opening the...
9
by: David Sworder | last post by:
Hi, I have a form that displays data (is that vague enough for you?). The data comes in on a thread-pool thread. Since the thread pool thread is not the same as the UI thread, the callback...
2
by: Howard Weiss | last post by:
I am writing a Managed C++ application. In my application, I create a modeless sub-form using New. I retain a pointer to this sub-form in my application. If the user closes this form, my...
2
by: John Savidge | last post by:
I have the following code in a class in my simple forms application, its in the early stages. I am a VB.net newbie and have a question I am hoping someone could help me on. Assuming I know how...
4
by: Larry Tate | last post by:
I have been at this for over a month and have finally broke down and decided to post having exhausted all other resources. I have tried all the methods of talking to controls on different...
2
by: Lucvdv | last post by:
To avoid a temporarily frozen user interface, I'm using a separate thread to fill a list with items found in a database (there can be from a few up to about 1000 or 1500 items). There seems to...
6
by: Gabriel Lozano-Morán | last post by:
I have always read that forms that are shown modally through ShowDialog() need to be disposed manually. Because they are not disposed automatically since you can still access the form after the...
2
by: John J. Hughes II | last post by:
I have a form with a socket which is handled by the beginReceive function. When the data is received it is in turn returned to the window for display. I have done this several times before and the...
7
by: =?Utf-8?B?R2lkaQ==?= | last post by:
Hi, In my Windows application, I'm using thread, and I've 2 questions: 1. I read that it's not recommended to use Thread.Abort(), so what is the best way to close the thread, in case the user...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...

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.