Disposed form still responds to events

B

BillE

Using vb.net 2008 windows forms.

How can a form which has been closed and disposed still attempt to handle a
custom event?

I have a form which inherits from a base form class.

The base class has a shared event which is raised when a property in the
base class changes (the property updates a shared variable value).

The form handles this event to refresh itself based on the new property
value.

This all works very nicely.

When I close the form I call me.dispose.

If the shared event in the base class is subsequently raised by another form
which inherits the same base class, I get an error in the form which was
already closed and disposed:
"Cannot access a disposed object."
The object refers to a combo box which is populated when the event fires, on
the form which has been closed and disposed.

I work around this by testing the IsDisposed property of the form when the
event fires, but I don't understand why this should be necessary.

Thanks
Bill
 
A

Armin Zingler

BillE said:
Using vb.net 2008 windows forms.

How can a form which has been closed and disposed still attempt to
handle a custom event?

By still being the target of the delegate.

I have a form which inherits from a base form class.

The base class has a shared event which is raised when a property in
the base class changes (the property updates a shared variable
value).

The form handles this event to refresh itself based on the new
property value.

This all works very nicely.

When I close the form I call me.dispose.

If it's not shown modally (ShowDialog), this (me.dispose) is not necessary
because done implicitly.
If the shared event in the base class is subsequently raised by
another form which inherits the same base class, I get an error in
the form which was already closed and disposed:
"Cannot access a disposed object."
The object refers to a combo box which is populated when the event
fires, on the form which has been closed and disposed.

I work around this by testing the IsDisposed property of the form
when the event fires, but I don't understand why this should be
necessary.

I'd remove the handler in the Form's FormClosed event.

Disposing has nothing to do with event handling. The form object can still
handle events. Disposing does not mean killing the form immediatelly. It's
just there to release some ressources ASAP (see the other long long thread
in this group). BTW, as long as the form still handles events, it won't be
collected by the GC.


Armin
 
B

BillE

Thanks! I understand now.

Armin Zingler said:
By still being the target of the delegate.



If it's not shown modally (ShowDialog), this (me.dispose) is not necessary
because done implicitly.


I'd remove the handler in the Form's FormClosed event.

Disposing has nothing to do with event handling. The form object can still
handle events. Disposing does not mean killing the form immediatelly. It's
just there to release some ressources ASAP (see the other long long thread
in this group). BTW, as long as the form still handles events, it won't be
collected by the GC.


Armin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top