473,880 Members | 1,870 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 4319

"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
13506
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
2513
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
3211
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
2160
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
13313
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
3999
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
1628
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
11456
by: Alan T | last post by:
private Interop.Word.Application _wordApp; What is the differences betwenn _wordApp.Quit(...) and _wordApp.Application.Quit(...) ?
5
3869
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
9772
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10713
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
10809
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,...
0
9550
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7104
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
5777
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5972
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4595
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
2
4192
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.