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;
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;