How to properly use Form.Show with Form.Close

K

kirk

I have a main form that can open a settings form. The settings form
has an "OK" and a "Cancel" button to call this.Close(). In some rare
instances "frmSettings.Instance" referencing the code below, returns
null. Am I going about this implementation the right way? I don't
want to use ShowDialog to simplify as it locks up the caller's UI.

// settings button handler in main form
try

{
frmSettings mySettingsDlg = frmSettings.Instance;

if (mySettingsDlg != null)
{
if (!mySettingsDlg.Visible)
{
mySettingsDlg.Show();
}
else
{
mySettingsDlg.Activate();
mySettingsDlg.Focus();
}
}
else
{
throw new Exception("Unable to obtain instance of Settings Dialog.
Please try again.");
}
}
catch (Exception err)
{
MessageBox.Show(err.Message, APP_TITLE, MessageBoxButtons.OK,
MessageBoxIcon.Error);
}

-------------------------------------------------------------------------------------------------------------------------------------
// singleton implementation in settings form

private static frmSettings frmSettingsInstance = null;
public static frmSettings Instance
{
get
{
if (frmSettingsInstance == null)
{
// null reference, go for new instantiation
frmSettingsInstance = new frmSettings();
return frmSettingsInstance;
}
else if (!frmSettingsInstance.IsDisposed)
{
// reference is not null and not disposed, return good reference
return frmSettingsInstance;
}
else
{
// reference is not null, but disposed
// return null
return null;
}
}
}

-------------------------------------------------------------------------------------------------------------------------------------
// code executed by "OK" and "Cancel" buttons in settings form
// close dlg
this.Close();

// dereference static instance to enable GC to completely dispose if
Show() was used
frmSettingsInstance = null;
 
P

Peter Duniho

I have a main form that can open a settings form. The settings form
has an "OK" and a "Cancel" button to call this.Close(). In some rare
instances "frmSettings.Instance" referencing the code below, returns
null. Am I going about this implementation the right way? I don't
want to use ShowDialog to simplify as it locks up the caller's UI.

If you use Form.Show() to show the form, rather than Form.ShowDialog(),
then when the form is closed, it is also disposed.

It's not really clear from the code you posted to know for sure what
exactly you're doing. You've left too much out, so things like just
how exactly you close/hide/dispose the form, and under what conditions
the instance of that form is retrieved, those details are mission.
However, it seems to me that one approach you might try is simply to
set "frmSettingsInstance" to null _before_ you close the form, rather
than after.

Assuming everything's happening in a single thread, that would probably
clear up whatever the problem is. If you have some sort of threading
issue, that isn't necessarily going to solve it and instead you should
look at where you should be synchronizing the code.

Pete
 

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