473,624 Members | 2,027 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Code to quit an application

Jay
In my C# code, I'm attempting to display a message box then quit the win form application I'm
writing if a certain type of error occurs when the application starts up.

In the main form's constructor - FormMain() - if this error occurs, I display a message box then
call Application.Exi t().

The problem is that I also need to have some code in my FormMain_FormCl osing event handler, but this
prevents Application.Exi t() from quitting the application. Is there another way of quiting an
application that doesn't call FormMain_FormCl osing?
Jan 24 '07 #1
7 4307

"Jay" <-wrote in message news:%2******** ********@TK2MSF TNGP03.phx.gbl. ..
In my C# code, I'm attempting to display a message box then quit the win
form application I'm
writing if a certain type of error occurs when the application starts up.

In the main form's constructor - FormMain() - if this error occurs, I
display a message box then
call Application.Exi t().

The problem is that I also need to have some code in my
FormMain_FormCl osing event handler, but this
prevents Application.Exi t() from quitting the application. Is there
another way of quiting an
application that doesn't call FormMain_FormCl osing?
If it's an eventhandler, you can unsubscribe it before exiting. You can
also set a variable, and let OnFormClosing check the variable and return
without calling the base classes (that calls event handlers) when you want a
fast exit.
>

Jan 25 '07 #2
Hi,

An alternative is to use the Form.Closing event instead of Form.FormClosin g.
The Closing event doesn't get raised when Application.Exi t is called, but
the FormClosing event does.

--
Dave Sexton
http://davesexton.com/blog
http://www.codeplex.com/DocProject (Sandcastle in VS IDE)

"Ben Voigt" <rb*@nospam.nos pamwrote in message
news:uC******** ******@TK2MSFTN GP03.phx.gbl...
>
"Jay" <-wrote in message news:%2******** ********@TK2MSF TNGP03.phx.gbl. ..
>In my C# code, I'm attempting to display a message box then quit the win
form application I'm
writing if a certain type of error occurs when the application starts up.

In the main form's constructor - FormMain() - if this error occurs, I
display a message box then
call Application.Exi t().

The problem is that I also need to have some code in my
FormMain_FormC losing event handler, but this
prevents Application.Exi t() from quitting the application. Is there
another way of quiting an
application that doesn't call FormMain_FormCl osing?

If it's an eventhandler, you can unsubscribe it before exiting. You can
also set a variable, and let OnFormClosing check the variable and return
without calling the base classes (that calls event handlers) when you want
a fast exit.
>>


Jan 25 '07 #3
One thing you can do is listen for the application closing windows
messages and disconnect any event handlers if a form close is detected

eg.
private const int WM_CLOSE = 0x0010;
private const int WM_QUERYENDSESS ION = 0x0011;
private const int WM_ENDSESSION = 0x0016;

protected override void WndProc(ref Message m)
{
if (m.Msg == WM_CLOSE || m.Msg == WM_QUERYENDSESS ION
|| m.Msg == WM_ENDSESSION)
{
//disconnet event handlers
}
base.WndProc(re f m);
}

On Jan 25, 2:14 pm, "Dave Sexton" <dave@jwa[remove.this]online.com>
wrote:
Hi,

An alternative is to use the Form.Closing event instead of Form.FormClosin g.
The Closing event doesn't get raised when Application.Exi t is called, but
the FormClosing event does.

--
Dave Sextonhttp://davesexton.com/bloghttp://www.codeplex.co m/DocProject(Sand castle in VS IDE)

"Ben Voigt" <r...@nospam.no spamwrote in messagenews:uC* *************@T K2MSFTNGP03.phx .gbl...
"Jay" <-wrote in messagenews:%2* *************** @TK2MSFTNGP03.p hx.gbl...
In my C# code, I'm attempting to display a message box then quit the win
form application I'm
writing if a certain type of error occurs when the application starts up.
In the main form's constructor - FormMain() - if this error occurs, I
display a message box then
call Application.Exi t().
The problem is that I also need to have some code in my
FormMain_FormCl osing event handler, but this
prevents Application.Exi t() from quitting the application. Is there
another way of quiting an
application that doesn't call FormMain_FormCl osing?
If it's an eventhandler, you can unsubscribe it before exiting. You can
also set a variable, and let OnFormClosing check the variable and return
without calling the base classes (that calls event handlers) when you want
a fast exit.
Jan 25 '07 #4
Jay
Thanks for your replies everyone. I'll set a variable as Ben suggested.

Now I have another problem. Application.Exi t() appear to do nothing when it's called anywhere in the
main form's constructor - FormMain(). The programme contines to run as if it wasn't called, and
stepping through with the debugger shows that it does nothing too. It appears to work anywere else,
however. Do you have any suggestions or alternatives, since the error checking is performed in the
constructor, and I must quit the application if the error occurs.

"Jay" <-wrote in message news:%2******** ********@TK2MSF TNGP03.phx.gbl. ..
In my C# code, I'm attempting to display a message box then quit the win form application I'm
writing if a certain type of error occurs when the application starts up.

In the main form's constructor - FormMain() - if this error occurs, I display a message box then
call Application.Exi t().

The problem is that I also need to have some code in my FormMain_FormCl osing event handler, but this
prevents Application.Exi t() from quitting the application. Is there another way of quiting an
application that doesn't call FormMain_FormCl osing?

Jan 25 '07 #5
Hi,

You shouldn't be performing the validation logic and throwing an exception
in the constructor of your Form unless you're simply validating arguments.
Do all of the appropriate argument and non-argument validation before
constructing an instance of the Form in the calling code, and decide then
whether you even need to construct the Form in the first place. If it's the
main form of your application then you won't even need to call
Application.Exi t - just let the thread exit on its own.

--
Dave Sexton
http://davesexton.com/blog
http://www.codeplex.com/DocProject (Sandcastle in VS IDE)

"Jay" <-wrote in message news:uZ******** ******@TK2MSFTN GP03.phx.gbl...
Thanks for your replies everyone. I'll set a variable as Ben suggested.

Now I have another problem. Application.Exi t() appear to do nothing when
it's called anywhere in the
main form's constructor - FormMain(). The programme contines to run as if
it wasn't called, and
stepping through with the debugger shows that it does nothing too. It
appears to work anywere else,
however. Do you have any suggestions or alternatives, since the error
checking is performed in the
constructor, and I must quit the application if the error occurs.

"Jay" <-wrote in message news:%2******** ********@TK2MSF TNGP03.phx.gbl. ..
In my C# code, I'm attempting to display a message box then quit the win
form application I'm
writing if a certain type of error occurs when the application starts up.

In the main form's constructor - FormMain() - if this error occurs, I
display a message box then
call Application.Exi t().

The problem is that I also need to have some code in my
FormMain_FormCl osing event handler, but this
prevents Application.Exi t() from quitting the application. Is there
another way of quiting an
application that doesn't call FormMain_FormCl osing?

Jan 25 '07 #6
Jay
Thanks for your help Dave.

Do you mean I should put the validation in main()? If so, is between Application.Set and
Application.Run a sensible place (see below)?

static void Main(){
Application.Ena bleVisualStyles ();
Application.Set CompatibleTextR enderingDefault (false);
//validation logic
...
Application.Run (new FormMain());
}
"If it's the main form of your application then you won't even need to call Application.Exi t - just
let the thread exit on its own."

It is the main form of my application. I'm not sure what you mean by "just let the thread exit on
its own". Do you mean I should use something like:

static void Main(){
Application.Ena bleVisualStyles ();
Application.Set CompatibleTextR enderingDefault (false);
//validation logic
...
if(validatesOk) Application.Run (new FormMain());
}

"Dave Sexton" <dave@jwa[remove.this]online.comwrote in message
news:O6******** ******@TK2MSFTN GP05.phx.gbl...
Hi,

You shouldn't be performing the validation logic and throwing an exception
in the constructor of your Form unless you're simply validating arguments.
Do all of the appropriate argument and non-argument validation before
constructing an instance of the Form in the calling code, and decide then
whether you even need to construct the Form in the first place. If it's the
main form of your application then you won't even need to call
Application.Exi t - just let the thread exit on its own.

--
Dave Sexton
http://davesexton.com/blog
http://www.codeplex.com/DocProject (Sandcastle in VS IDE)

"Jay" <-wrote in message news:uZ******** ******@TK2MSFTN GP03.phx.gbl...
Thanks for your replies everyone. I'll set a variable as Ben suggested.

Now I have another problem. Application.Exi t() appear to do nothing when
it's called anywhere in the
main form's constructor - FormMain(). The programme contines to run as if
it wasn't called, and
stepping through with the debugger shows that it does nothing too. It
appears to work anywere else,
however. Do you have any suggestions or alternatives, since the error
checking is performed in the
constructor, and I must quit the application if the error occurs.

"Jay" <-wrote in message news:%2******** ********@TK2MSF TNGP03.phx.gbl. ..
In my C# code, I'm attempting to display a message box then quit the win
form application I'm
writing if a certain type of error occurs when the application starts up.

In the main form's constructor - FormMain() - if this error occurs, I
display a message box then
call Application.Exi t().

The problem is that I also need to have some code in my
FormMain_FormCl osing event handler, but this
prevents Application.Exi t() from quitting the application. Is there
another way of quiting an
application that doesn't call FormMain_FormCl osing?
Jan 25 '07 #7
Hi Jay,

You got it :)

--
Dave Sexton
http://davesexton.com/blog
http://www.codeplex.com/DocProject (Sandcastle in VS IDE)

"Jay" <-wrote in message news:es******** ******@TK2MSFTN GP03.phx.gbl...
Thanks for your help Dave.

Do you mean I should put the validation in main()? If so, is between
Application.Set and
Application.Run a sensible place (see below)?

static void Main(){
Application.Ena bleVisualStyles ();
Application.Set CompatibleTextR enderingDefault (false);
//validation logic
...
Application.Run (new FormMain());
}
"If it's the main form of your application then you won't even need to
call Application.Exi t - just
let the thread exit on its own."

It is the main form of my application. I'm not sure what you mean by "just
let the thread exit on
its own". Do you mean I should use something like:

static void Main(){
Application.Ena bleVisualStyles ();
Application.Set CompatibleTextR enderingDefault (false);
//validation logic
...
if(validatesOk) Application.Run (new FormMain());
}

"Dave Sexton" <dave@jwa[remove.this]online.comwrote in message
news:O6******** ******@TK2MSFTN GP05.phx.gbl...
Hi,

You shouldn't be performing the validation logic and throwing an exception
in the constructor of your Form unless you're simply validating arguments.
Do all of the appropriate argument and non-argument validation before
constructing an instance of the Form in the calling code, and decide then
whether you even need to construct the Form in the first place. If it's
the
main form of your application then you won't even need to call
Application.Exi t - just let the thread exit on its own.

--
Dave Sexton
http://davesexton.com/blog
http://www.codeplex.com/DocProject (Sandcastle in VS IDE)

"Jay" <-wrote in message news:uZ******** ******@TK2MSFTN GP03.phx.gbl...
>Thanks for your replies everyone. I'll set a variable as Ben suggested.

Now I have another problem. Application.Exi t() appear to do nothing when
it's called anywhere in the
main form's constructor - FormMain(). The programme contines to run as if
it wasn't called, and
stepping through with the debugger shows that it does nothing too. It
appears to work anywere else,
however. Do you have any suggestions or alternatives, since the error
checking is performed in the
constructor, and I must quit the application if the error occurs.

"Jay" <-wrote in message
news:%2******* *********@TK2MS FTNGP03.phx.gbl ...
In my C# code, I'm attempting to display a message box then quit the win
form application I'm
writing if a certain type of error occurs when the application starts up.

In the main form's constructor - FormMain() - if this error occurs, I
display a message box then
call Application.Exi t().

The problem is that I also need to have some code in my
FormMain_FormC losing event handler, but this
prevents Application.Exi t() from quitting the application. Is there
another way of quiting an
application that doesn't call FormMain_FormCl osing?

Jan 25 '07 #8

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

Similar topics

242
13323
by: James Cameron | last post by:
Hi I'm developing a program and the client is worried about future reuse of the code. Say 5, 10, 15 years down the road. This will be a major factor in selecting the development language. Any comments on past experience, research articles, comments on the matter would be much appreciated. I suspect something like C would be the best based on comments I received from the VB news group. Thanks for the help in advance James Cameron
2
2506
by: Bob Hynes | last post by:
Hi All, This code is running in WinNT/Access97. I'm looking for some help with getting this code to do two(2) things better then it does now and I'm not sure how to do it. 1. On the 'objWord.Application.Quit' statement, it asks if I want to save changes and I would like it to not ask that and I don't know what the exact code is to do that. 2. On the 'objWord.MailMerge.Execute' statement the printer dialog window pops up and I have to...
2
3202
by: hsuntn | last post by:
I'm working with Office Application objects in a client/server WinForm application which allows the user to access remotely stored files. When the user opens the remote file, it is downloaded to their machine, and it is opened in the appropriate Office app. I've hooked the various BeforeSave events to let me know if/when they've made modifications to the file, so I can mark it as dirty. However, I would like to know when the Office...
2
2147
by: touf | last post by:
Hi, I use the folowing code to create a new Excel file using a query It creates an excel file and save it on the disc, but When I double-clic on it (explorer) it open Excel and bloc,(don't display the data), it does the same with all excel files. I've found that it creates a process called Excel, when I end it (Windows Task Manager) the problem is resolved. I think that I need somewhere to kill the process..(I use oExcel.Quit() but
5
13300
by: mzdude | last post by:
I've just started playing with Python. Installed 2.5 on Windows XP system. I'm working through some of the examples in Programming Python 3ed by Mark Lutz. Given the following example when the Quit All button action is assigned to root.quit the windows aren't dismissed or destroyed. The application appears to terminate, but the windows remain. When the action is assigned to root.destroy, the windows are closed. Questions: 1) Which way is...
0
3982
by: Alan T | last post by:
I tried to close the word document object and word application: private Interop.Word.Application WordApp; private Interop.Word.Document WordDoc; // close word and quit word application WordDoc.Close(ref _newMissing, ref _newMissing, ref _newMissing); WordApp.Application.Quit(ref _notTrue, ref _newMissing, ref _newMissing);
0
1619
by: Alan T | last post by:
private Interop.Word.Application _mergeWordApp; I tried to quit the Word application: _mergeWordApp.Application.Quit(ref _notTrue, ref _newMissing, ref _newMissing); However, I got compilation warning -
2
11448
by: Alan T | last post by:
private Interop.Word.Application _wordApp; What is the differences betwenn _wordApp.Quit(...) and _wordApp.Application.Quit(...) ?
5
3857
by: vamsioracle | last post by:
Hi all, I have a problem with the ult_smtp package. Let me explain how the structure of my code is procedure------------ begin declarations of variables and cursors ........................ .....................
0
8236
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8679
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8621
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8335
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6110
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5563
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4174
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2606
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1785
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.