473,404 Members | 2,178 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

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

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

Mar 29 '07 #1
6 4192
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 Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<ti***********@gmail.comwrote in message
news:11**********************@o5g2000hsb.googlegro ups.com...
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

Mar 29 '07 #2
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
}
}
}

Mar 30 '07 #3
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

Mar 30 '07 #4
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
Mar 30 '07 #5
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

Mar 30 '07 #6
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(fals e);

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

Mar 30 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

29
by: lori3laz | last post by:
How do you disable the right click>view source option on web pages so people can't view your coding and copy it? What's the html I need to include in my website to utilize this feature? Thank...
8
by: edster | last post by:
A *lot* of our teams code is being deleted from the InitializeComponent() section of a winform, such as Grid settings, event handler hooks, layout settings etc. This mainly seems to happen to 3rd...
9
by: sdbranum | last post by:
Tab pages, which I have added to forms in design view, are occassionally reordered without warning. Other than hard-coding the tab order by clearing then readding these same tab pages outside...
3
by: Venkat Venkataramanan | last post by:
Hello: I have a winform that has a private subroutine defined as: Private Sub ApplyFilter() This sub refers to objects that get instantiated in the winform's new method. However, when I...
0
by: Amiram Korach | last post by:
When you create a MDI form, you can attach a main menu to the parent and to the child. When a child form is active, its menu is merged with the parent menu. The problem is: when the forms are...
5
by: sherifffruitfly | last post by:
Hi all, I'm certain this is stupid, but I'd like to know how to keep a form open for, say, longer than 1ms. Here's the code I'm using to open my form: TestModelFileReaderForm modelInfo = new...
9
by: MJ | last post by:
I have a strange (to me) problem with a c# forms application that I am developing. The application is installed on approxmatly 30 Windows XP machines and until recently they all worked perfectly....
3
by: raylopez99 | last post by:
Oh, I know, I should have provided complete code in console mode form. But for the rest of you (sorry Jon, just kidding) I have an example of why, once again, you must pick the correct entry point...
11
by: Simon van Beek | last post by:
Dear reader, By opening an application I get always the main Access window with the closing cross in the above right corner. Is there a possibility to make this closing cross invisible? ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.