Problem closing down the application

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a problem closing down the application, i haven't dont much in the
project yet, so it contains a program.cs file (generated by Visual studio)
with the main function. Also my main form, now my problem is that, in afew
places in my mainForm i want to Exit the application and close the probram,
when i try:
Application.Exit();
nothing happens, it keeps running. Then i also tried to add
private void mainForm_FormClosed(object sender, FormClosedEventArgs e)
{
Application.Exit();
}

and called it from one of my funcctions with this:
FormClosedEventArgs mArgs = new
FormClosedEventArgs(CloseReason.ApplicationExitCall);
mainForm_FormClosed(this, mArgs);

but it still continues to run.
All this is in the loading sequence of the form, i am loading alot of
pictures from an resourcefile, when a picture won't load i need to exit the
entire application. Any suggestions?
 
Yu could always do this step in the Load event, and throw an exception,
as this should interrupt the Form.Show() / Form.ShowDialog() /
Application.Run() method? Your Program.cs can then catch that exception
and handle appropriately, as so:

static void Main() {
try {
using (Form f = new Form()) {
f.Load += delegate { throw new Exception("Bad day
at the office"); };
f.ShowDialog();
}
} catch (Exception ex) {
MessageBox.Show(ex.Message, "Whoops!",
MessageBoxButtons.OK, MessageBoxIcon.Stop);
}
}

Personally, I prefer exiting cleanly where possible (through the bottom
of Main()) - however, if it gets desparate you could always try
Environment.FailFast() - but note that this is /very/ brutal.

Marc
 
addendum: throwing an exception from the constructor would also work;
sorry - force of habit for me to load things as late as possible ;-p

also - if using the Load event, you would handle this inside your
custom form, not Main() - my code was illustrative only - i.e.

public class MyForm : Form {
public MyForm() {
InitializeComponents(); // or whatever the IDE inserts
Load+=MyForm_Load;
}
private void MyForm_Load(object sender, EventArgs args) {
// load the objects and barf if necessary
}
}

Marc
 
And for completeness, I think I know what is going wrong: you are
calling Application.Exit from the constructor, right?
From MSDN2, Application.Exit():
"Informs all message pumps that they must terminate, and then closes
all application windows after the messages have been processed."

Well, if you are still in the ctor of the first form, then the pump
hasn't started, and the window isn't open - so Application.Exit() has
nothing interesting to do. I tested this with sample code, and indeeed,
Application.Exit() in the ctor of the form still allows that form to be
subsequently displayed (not sure if this is a bug or not ;-p) - but any
*existing* forms will commit suicide. Moving this call to the Load
event makes it behave as expected (except that it flashes on screen),
but the exception route (in either the constructor or Load event) works
more cleanly, as the form is never shown at all.

Marc
 
Patrick said:
I have a problem closing down the application, i haven't dont much in the
project yet, so it contains a program.cs file (generated by Visual studio)
with the main function. Also my main form, now my problem is that, in afew
places in my mainForm i want to Exit the application and close the probram,
when i try:
Application.Exit();
nothing happens, it keeps running. Then i also tried to add
private void mainForm_FormClosed(object sender, FormClosedEventArgs e)
{
Application.Exit();
}

and called it from one of my funcctions with this:
FormClosedEventArgs mArgs = new
FormClosedEventArgs(CloseReason.ApplicationExitCall);
mainForm_FormClosed(this, mArgs);

but it still continues to run.
All this is in the loading sequence of the form, i am loading alot of
pictures from an resourcefile, when a picture won't load i need to exit the
entire application. Any suggestions?
if you close your main form (the one opened via application.run(myform))
then the application will exit.

this.Close();

JB
 
Back
Top