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

Disposed form still responds to events

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
Oct 16 '08 #1
2 4076
"BillE" <be****@datamti.comschrieb
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

Oct 16 '08 #2
Thanks! I understand now.

"Armin Zingler" <az*******@freenet.dewrote in message
news:ew**************@TK2MSFTNGP06.phx.gbl...
"BillE" <be****@datamti.comschrieb
>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

Oct 16 '08 #3

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

Similar topics

7
by: Ryan Park | last post by:
Hi, //SITUATION I got a panel control that hold a certain position on a form. Every controls or UIs are on this panel. At certain situation, I called dispose() method of this panel control...
1
by: mfdatsw1 | last post by:
I have a .NET VB form that contains a Web Browser control. In the document in the Web Browser I have JavaScript that responds to certain click events within nodes (not buttons or other controls)....
4
by: Sean Connery | last post by:
I have a Microsoft UI Process Application Block that is controlling child forms in an MDI parent container. The views node in the app.config file has been set to stayOpen=false. Because there...
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...
8
by: Tim Geiges | last post by:
Since I am being challenged with learning c# I figured I could pass some of the pain on to you guys :-) I have another question(this one is important for me to fix before I can get my app to Beta)...
5
by: theinvisibleGhost | last post by:
I'm having a problem that occurs at random in my app. I get an exception "Cannot Access a disposed object" In MSCorLib when calling boolean Change (int32, int32) Stack trace reveals...
4
by: wirawan | last post by:
hi all i had some problems and need your sugestion to solve it. why event of controls that i have been disposed still alive and trigger method had been bind to it. best regards wirawan --
7
by: Boni | last post by:
Dear all, if I do myform.close() is it disposed or not? is it correct do following? myForm.close() Info=SomeControlOntheForm.text This seems to work. But am not sure that it is waterproof....
5
by: aine_canby | last post by:
Hi, The problem is that the line - this.Invoke(new MyDelegate(Function), args); // this = MainForm is being called after the line - terminatePopulate = true; therefore I get an exception...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.