right after InitializeComponent(), how do I close the application?

  • Thread starter Thread starter titan.nyquist
  • Start date Start date
T

titan.nyquist

Right after InitializeComponent() is run, I do some error checking...
when an (very bad) error occurs I show it to the user and I want to
close down the application.

QUESTION: How do I close the application down? I am assuming I can
do it nearly right after InitializeComponent() is run, in the same
code location it exists in.

I'm new, so any help is great. Any articles on the topic would help,
too.

Titan
 
Titan,

You can just call the static Exit method on the Application class. This
assumes that this is a windows forms app. If it is a console app, then you
will want to call the Exit method on the Environment class.

It should be noted that you should not leave it to your components to
perform this operation. It screams of poor design. If anything, the
hosting executable should be informed in some way (through an exception, a
return value, something) that an error has occured, and then you can just
have the host application exit itself.

Hope this helps.
 
Nicholas,

That does not seem to work.

I just created the most simple WIndows application, and added a call
to Application.Exit() immediately after InitializeComponent(), and it
seems to do nothing. The application stays loaded until the user
clicks to close it. The code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ShutDownOnImmediateFatalErrorTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Application.Exit(); // <---- this is the only line of code i added
}
}
}
 
If I try to run Close(), like so:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ShutDownOnImmediateFatalErrorTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Close(); // <---- here's where I call Close()
}
}
}

This will close down Form1, but too prematurely. Then
Application.Run(new Form1()) in Main() has lost a resource it uses,
and cannot access the disposed object. So, although this works
somewhat, it crashes the program.

I'm so new at this, can you give me any insight on how to gracefully
close down the application?

Titan
 
seems an odd place to want to exit... however, you could perhaps throw
an exception (caught from your code), or set a property to say "I'm
invalid".

At last ditch, you could look at Process.GetCurrentProcess().Kill() or
Environment.FailFast(), but neither of these are very graceful...

Marc
 
Apparantly, Application.Exit() does NOTHING inside the main form
(Form1 in my code above) constructor.

Dave Sexton, in this thread: http://www.thescripts.com/forum/thread592698.html,
says the error checking should be handled BEFORE the main form is even
created. This makes sense, especially if all you are going to do is
shut down the main form, the one you just created for nothing if a
fatal error occurs.

I'll try it and post back...

Titan
 
Dave Sexton seems to have the answer. This works (this code from the
default Windows application, same as the code above only this time it
is the Program.cs file):


using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace ShutDownOnImmediateFatalErrorTest
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);

bool fatalError = true;
if (fatalError == false)
{
Application.Run(new Form1()); // <--- enclosed Application.Run()
inside an error checking if statement
// it never runs if a fatal error occurs, and therefore no need
to close it down
}
}
}
}


(Note: I did not change the Form1.cs file.)

Titan
 

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

Back
Top