Disposed form still responds to events

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • BillE

    #1

    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


  • Armin Zingler

    #2
    Re: Disposed form still responds to events

    "BillE" <belgie@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

    Comment

    • BillE

      #3
      Re: Disposed form still responds to events

      Thanks! I understand now.

      "Armin Zingler" <az.nospam@free net.dewrote in message
      news:ewSIF95LJH A.4600@TK2MSFTN GP06.phx.gbl...
      "BillE" <belgie@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
      >

      Comment

      Working...