473,387 Members | 1,492 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,387 software developers and data experts.

Opening and Closing a form error : Cannot access a disposed object named "frmImage".



I use the following code to open up a form.

-------------------------------------------------------

If fImage Is Nothing Then
fImage = New frmImage
End If

fImage.Show()

-------------------------------------------------------

It works the first time, but when the form is closed and this piece of code runs again I want to check to see if it is nothing and then if it is nothing then create a new instance of the form and show it. Otherwise just show the form. Well when the form is closed by the user the form becmome "disposed" instead of nothing. Is this the result of ..NET Garbage Collection, and that fImage would eventually become nothing?

On top of this, how can I check to see if the object is disposed?

This is the error when closing the form and running that code above again:

Run-time exception thrown : System.ObjectDisposedException - Cannot access a disposed object named "frmImage".

Jul 21 '05 #1
5 6108
D. Witherspoon

Setting something to nothing does nothing to delete it.
It only deletes the reference it has to its object

When you want to know if you still can use a form than you can use the

Isdisposed.
http://msdn.microsoft.com/library/de...posedtopic.asp

I hope this helps,

Cor
Jul 21 '05 #2
Hi D Witherspoon,

Yes, when an object like windowsform is closed it will be marked disposed
until the garbage collector has released it completely, at which time it
will be null.

You can check if Windows Form is disposed by checking its IsDisposed
property

if(fImage == null || !fImage.IsDisposed)
fImage = new frmImage();

--
Happy Coding!
Morten Wennevik [C# MVP]
Jul 21 '05 #3
Morten,

This isn't exactly correct. The garbage collector won't release an object
if there is still a strong reference to it. For example, in your code
snippit below, fImage will never be set to null by the GC, since that is
the strong reference keeping it alive.

-Chris

--------------------
| Hi D Witherspoon,
|
| Yes, when an object like windowsform is closed it will be marked disposed

| until the garbage collector has released it completely, at which time it
| will be null.
|
| You can check if Windows Form is disposed by checking its IsDisposed
| property
|
| if(fImage == null || !fImage.IsDisposed)
| fImage = new frmImage();
|
| --
| Happy Coding!
| Morten Wennevik [C# MVP]
|

Jul 21 '05 #4
"D Witherspoon" <dw**********@noway.org> schrieb:
If fImage Is Nothing Then

fImage = New frmImage
End If

fImage.Show()

-------------------------------------------------------

It works the first time, but when the form is closed and this piece of code
runs again I want to check to see if it is nothing and then if it is nothing
then create a new instance of the form and show it. Otherwise just show the
form. Well when the form is closed by the user the form becmome "disposed"
instead of nothing. Is this the result of .NET Garbage Collection, and that
fImage would eventually become nothing?

On top of this, how can I check to see if the object is disposed?

This is the error when closing the form and running that code above again:

Run-time exception thrown : System.ObjectDisposedException - Cannot access a
disposed object named "frmImage".<<<

See:

<URL:http://groups.google.de/groups?selm=eILSwL4CFHA.2072%40TK2MSFTNGP10.phx.gb l>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Jul 21 '05 #5
You are of course correct. I had something else in mind, but it turned
out wrong *sigh*

--
Happy Coding!
Morten Wennevik [C# MVP]
Jul 21 '05 #6

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

Similar topics

3
by: D Witherspoon | last post by:
I use the following code to open up a form. ------------------------------------------------------- If fImage Is Nothing Then fImage = New frmImage End If fImage.Show()
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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
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...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.