473,395 Members | 1,941 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,395 software developers and data experts.

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.Exit().

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

"Jay" <-wrote in message news:%2****************@TK2MSFTNGP03.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.Exit().

The problem is that I also need to have some code in my
FormMain_FormClosing event handler, but this
prevents Application.Exit() from quitting the application. Is there
another way of quiting an
application that doesn't call FormMain_FormClosing?
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.FormClosing.
The Closing event doesn't get raised when Application.Exit 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.nospamwrote in message
news:uC**************@TK2MSFTNGP03.phx.gbl...
>
"Jay" <-wrote in message news:%2****************@TK2MSFTNGP03.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.Exit().

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

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_QUERYENDSESSION = 0x0011;
private const int WM_ENDSESSION = 0x0016;

protected override void WndProc(ref Message m)
{
if (m.Msg == WM_CLOSE || m.Msg == WM_QUERYENDSESSION
|| m.Msg == WM_ENDSESSION)
{
//disconnet event handlers
}
base.WndProc(ref 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.FormClosing.
The Closing event doesn't get raised when Application.Exit is called, but
the FormClosing event does.

--
Dave Sextonhttp://davesexton.com/bloghttp://www.codeplex.com/DocProject(Sandcastle in VS IDE)

"Ben Voigt" <r...@nospam.nospamwrote in messagenews:uC**************@TK2MSFTNGP03.phx.gbl. ..
"Jay" <-wrote in messagenews:%2****************@TK2MSFTNGP03.phx.gb l...
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.Exit().
The problem is that I also need to have some code in my
FormMain_FormClosing event handler, but this
prevents Application.Exit() from quitting the application. Is there
another way of quiting an
application that doesn't call FormMain_FormClosing?
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.Exit() 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****************@TK2MSFTNGP03.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.Exit().

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

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.Exit - 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**************@TK2MSFTNGP03.phx.gbl...
Thanks for your replies everyone. I'll set a variable as Ben suggested.

Now I have another problem. Application.Exit() 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****************@TK2MSFTNGP03.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.Exit().

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

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.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(fals e);
//validation logic
...
Application.Run(new FormMain());
}
"If it's the main form of your application then you won't even need to call Application.Exit - 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.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(fals e);
//validation logic
...
if(validatesOk) Application.Run(new FormMain());
}

"Dave Sexton" <dave@jwa[remove.this]online.comwrote in message
news:O6**************@TK2MSFTNGP05.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.Exit - 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**************@TK2MSFTNGP03.phx.gbl...
Thanks for your replies everyone. I'll set a variable as Ben suggested.

Now I have another problem. Application.Exit() 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****************@TK2MSFTNGP03.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.Exit().

The problem is that I also need to have some code in my
FormMain_FormClosing event handler, but this
prevents Application.Exit() from quitting the application. Is there
another way of quiting an
application that doesn't call FormMain_FormClosing?
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**************@TK2MSFTNGP03.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.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(fals e);
//validation logic
...
Application.Run(new FormMain());
}
"If it's the main form of your application then you won't even need to
call Application.Exit - 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.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(fals e);
//validation logic
...
if(validatesOk) Application.Run(new FormMain());
}

"Dave Sexton" <dave@jwa[remove.this]online.comwrote in message
news:O6**************@TK2MSFTNGP05.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.Exit - 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**************@TK2MSFTNGP03.phx.gbl...
>Thanks for your replies everyone. I'll set a variable as Ben suggested.

Now I have another problem. Application.Exit() 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****************@TK2MSFTNGP03.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.Exit().

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

Jan 25 '07 #8

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

Similar topics

242
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...
2
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...
2
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...
2
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...
5
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...
0
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 ...
0
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...
2
by: Alan T | last post by:
private Interop.Word.Application _wordApp; What is the differences betwenn _wordApp.Quit(...) and _wordApp.Application.Quit(...) ?
5
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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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,...
0
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...
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...

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.