By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,852 Members | 955 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,852 IT Pros & Developers. It's quick & easy.

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

P: n/a


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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.