Aborting app during form load

M

Mark Stevens

I am having a problem aborting an application during startup. The
Application.Exit() function is not terminating the application
correctly.

The app checks the registry to see if it has been configured (i.e.
from a previous run). If it has not then it displays the file open
dialog to locate the application database. If the database is invalid
then a message box is displayed and the user is asked if they want to
try selecting another file.

Now this is where the problem starts. If the app is not configured
and the user selects Cancel in the message box then I want the app to
exit. However, it does not. Debugging the code shows that the app
gets through to the Application.Exit() but it does not exit.

I have Googled and checked the knowledge base and can find nothing
that helps.

The logic is as follows:

private void frmMain_Load(object sender, EventArgs e)
{
try
{
// Do some form initialisation and read config from registry
if (!configured) // Values from registry.
{
bool retrying = true;
while (retrying)
{
if (dlgOpenFile.ShowDialog() == DialogResult.OK)
{
// Do some check etc.
}
else
{
if (DialogResult.No == MessageBox.Show("Retry"))
{
Close();
Application.Exit();
}
}
}
}
}
catch
{
// Process the error.
}
}

Thanks in advance,
Mark
--
|\ _,,,---,,_ A picture used to be worth a
ZZZzzz /,`.-'`' -. ;-;;, thousand words - then along
|,4- ) )-,_. ,\ ( `'-' came television!
'---''(_/--' `-'\_)

Mark Stevens www.thepcsite.co.uk
 
F

Florian Fordermaier

Hi Mark,

I think, if you put your initialization code in your static main
function you can exit the application by simply returning from main, if
init failed. If initialization was OK, then you could start your forms
application with your form by calling Application.Run(new frmMain());

Florian
 
M

Mark Stevens

I think, if you put your initialization code in your static main
function you can exit the application by simply returning from main, if
init failed. If initialization was OK, then you could start your forms
application with your form by calling Application.Run(new frmMain());

Thought about that. My aim is to provide a method for the user to
attempt to recover to a "good" configuration. This requires the use
of forms hence the initialisation in the form creation.

Cheers,
Mark
--
|\ _,,,---,,_ A picture used to be worth a
ZZZzzz /,`.-'`' -. ;-;;, thousand words - then along
|,4- ) )-,_. ,\ ( `'-' came television!
'---''(_/--' `-'\_)

Mark Stevens www.thepcsite.co.uk
 
M

Mark Stevens

if (DialogResult.No == MessageBox.Show("Retry"))
{
Close();
Application.Exit();
}

For those who are interested, I've cracked it ! Add a return
statement after the Application.Exit();
--
|\ _,,,---,,_ A picture used to be worth a
ZZZzzz /,`.-'`' -. ;-;;, thousand words - then along
|,4- ) )-,_. ,\ ( `'-' came television!
'---''(_/--' `-'\_)

Mark Stevens (mark at thepcsite fullstop co fullstop uk)
 

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