Hi,
I am creating a multi-winform project. Everything has been working
fine, except after I perform an update to the database and decide to
Exit the winform after the update operation is complete.
I have error handling in my code but not on the dispose method. I call
the dispose method exiting the application using the code below but
results in this error message
"An unhandled exception of type 'System.NullReferenceException'
occurred in system.management.dll
Additional information: Object reference not set to an instance of an
object."
I am may be using the dispose method incorrectly but I can't seem to
locate the issue. Any advice or hints is greatly appreaciated.
private void mnuExit_Click(object sender, System.EventArgs e)
{
if (MessageBox.Show("Are you sure you want to exit?","Exit",
MessageBoxButtons.YesNo) == DialogResult.Yes)
{
this.Dispose();
}
}
protected override void Dispose( bool disposing )
{
if (!bOK)
{
if (MessageBox.Show("You have not updated the current data. Would you
like to before exiting?", "Exit",MessageBoxButtons.YesNo ) ==
DialogResult.Yes)
{
return;
}
}
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
Thanks in advance
Mark
FUnky - 07 Jul 2006 06:39 GMT
I don't think it is a good idea to call Dispose on menuExit_Click. Instead
call this.Close() there and inside the form_Closing event do your 'you have
not updated the current data' validation and set e.cancel = true, if the
user likes to update it.
William Sullivan - 07 Jul 2006 13:54 GMT
First, the creator of the window is responsible for calling Dispose on it,
not the window itself. Additionally, don't put any logic in the Dispose
method. You are only supposed to be releasing unmanaged resources held by
the form object there (and the only one held by forms (unless you add more)
is their window handle, which is released by calling base.Dispose()). Move
all your logic into your button handler. When the user indicates that the
form should be closed, use the Close() method to hide it from view; don't
call Dispose(). Whatever object opened the form in the first place will have
to determine whether or not its time to dispose the form.
> Hi,
>
[quoted text clipped - 52 lines]
>
> Mark