ObjectDisposedException

H

Hilton

Hi,

Here is the code:

DialogResult dialogResult = dgid.ShowDialog ();

XYZInfo xyzInfo = dgid.XYZInfo;

dgid.ShutDown ();
dgid.Dispose();

Pretty strightforward. The dialog has Windows timer to show the current
time. ShutDown calls this.timer.Enabled = false;. Sometimes, I get an
ObjectDisposedException. My thinking is that the dialog's timer adds a
(tick) message to the queue. ShutDown() disables the timer, and Dipose()
nukes the dialog. However, that tick message is still sitting in the queue.
When Windows gets around to processing the tick message, the form has been
disposed and I get the ObjectDisposedException. Does this sound right?

Assuming I am on the right track, how should I dispose of this form safely
so that no timer messages are left in the queue? Secondly, do I really need
to call Dispose or should I just let nature (GC) runs its course?

Thanks,

Hilton
 
H

Hilton

Hmmm, I just tried to reproduce a short test case and couldn't get this
exception. I'll keep plugging away here and report back what I find, but it
might not be timer related after all.

Thanks,

Hilton
 
H

Hilton

Oooo, tricky... I had "this.inputPanel.EnabledChanged += ..." in the dialog
and didn't do the "-=" on closing. The form was disposed after closing (see
code below), but when the input panel was tapped (aka enabled) at any other
time, bang!

Nothing like a short test case to prove or disprove a theory.

BTW: I also folded the shutdown call into the OnClosing, so things are
working well and are a little cleaner.

Hilton
 
H

Hilton

Simon,

That would allow the dialog to be shown and closed multiple times.
Alternatively, we could do the "+=" in ShowDialog and the "-=" in
Closed/Closing.

I tend to favor the ShowDialog/Closing combo, here's why. Let's say that
you do the "+=" in the constructor and "-=" in Dispose, then the method will
be called even when the dialog box is not being shown (and not yet
Disposed); i.e. anytime the SIP is enabled (expanded). If you have several
dialog boxe doing this and each EnabledChanged method calls
"LayoutControls", there would be an unnecessary delay when tapping the SIP.

Hilton
 
S

Simon Hart [MVP]

Unsubscribing from the SIP in the Closing\Closed method is no different from
unsubscribing from it in the Dispose method in relation to your arguments
which were :-
Let's say that
you do the "+=" in the constructor and "-=" in Dispose, then the method will
be called even when the dialog box is not being shown (and not yet
Disposed); i.e. anytime the SIP is enabled (expanded). If you have several
dialog boxe doing this and each EnabledChanged method calls
"LayoutControls", there would be an unnecessary delay when tapping the SIP

If you call a sub window, (B) from within this window(A) showing/hiding the
SIP will still call your event in the calling window (A).
--
Simon Hart
Visual Developer - Device Application Development MVP
http://simonrhart.blogspot.com
 
H

Hilton

Correct, but I never said that A would call ShowDialog on B. I just said
that you have created (and possibly shown) multiple windows.

e.g.
OptionsDlg optionsDlg;

void ShowOptionsDlg ()
{
// Create a single OptionsDlg and keep it around, showing it when
necessary - singletonish :)
if (this.optionsDlg == null)
{
this.optionsDlg = new OptionsDlg ();
}
this.optionsDlg.ShowDialog ();
}

After showing the options dialog once, its EnabledChanged method will be
called everytime the SIP is enabled, whether or not the OptionsDlg is being
displayed at the time -assuming the "-=" is done in the Dispose(). That's
why I suggested doing the "+=" in the ShowDialog and the "-=" in the
Closing/Closed.

Hilton
 

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