473,545 Members | 2,714 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Application.Exi t() fails

Pardon me if this is not the optimum newsgroup for this post, but it's
the only .NET newsgroup I read and I'm certain someone here can help me.

I have a C# program that checks for an error condition and if it finds
it it notifies the user with a MessageBox and then on the next line of
code (example in a minute) it calls Application.Exi t().

To my astonishment, I stepped through the code with the debugger, and
watched it call Application.Exi t() and then proceed to the next
statement.

Here's the code:

string s = "Some error message..."

if (!lNames.isVali dLanguage(ref strLanguage))
{
MessageBox.Show (s, "AppName", MessageBoxButto ns.OK,
MessageBoxIcon. Exclamation );

Application.Exi t();
}

As I mentioned, I stepped through the code and even stepped through it
in disassembly mode to make sure the Exit function was actually being
called.

My workaround is to replace Application.Exi t() with:

throw new System.Exceptio n();

And then catch it in Main, and call Application.Exi t() there.

[STAThread]
static void Main()
{
try
{
Application.Run (new frmITQ());
}
catch
{
Application.Exi t();
}
}

Does anyone have a clue for me? (Yes, I know I should make a custom
exception and test specifically for it, but this is just a
troubleshooting technique and I haven't completely given up on
Application.Exi t() yet.)
-- Rick
Jul 21 '05 #1
1 8494
lol. Most people have problems getting their programs to run rather than
exit ;-)

Here's an extract from the help about the Application.Exi t() method:
~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~
This method stops all running message loops on all threads and closes all
windows of the application. This method does not force the application to
exit.
~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~

From what I understand, Application.Exi t is not a way to stop your app there
and then (Like "End" in VB6). Instead it is used to signal all threads and
forms that they should close and allow any cleanup code to run (including
code after the call to exit).

My advice is to structure your program so that the call to Application.Exi t
is the last thing you do (i.e. Skip any following code if the logon fails).

Below is a quick example of what I mean (it was quickly put together, so
excuse bad habits)
~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~
Public Sub Main()
' Run Main Application - will start the GUI loop
Application.Run (New Form1())

' Code execution will reach here when Form1 is closed or after a call to
Application.exi t and all cleanup has finished

End Sub

Private Sub Form1_Load(ByVa l sender As Object, ByVal e As System.EventArg s)
Handles MyBase.Load
' Do processing....
' Do Logon .... if it fails then exit
If Not Logon() Then
Application.Exi t()
Exit Sub
End If

' Do rest of initialization processing
End Sub
~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~
"Guinness Mann" <GM***@dublin.c om> wrote in message
news:MP******** *************** *@news.newsguy. com...
Pardon me if this is not the optimum newsgroup for this post, but it's
the only .NET newsgroup I read and I'm certain someone here can help me.

I have a C# program that checks for an error condition and if it finds
it it notifies the user with a MessageBox and then on the next line of
code (example in a minute) it calls Application.Exi t().

To my astonishment, I stepped through the code with the debugger, and
watched it call Application.Exi t() and then proceed to the next
statement.

Here's the code:

string s = "Some error message..."

if (!lNames.isVali dLanguage(ref strLanguage))
{
MessageBox.Show (s, "AppName", MessageBoxButto ns.OK,
MessageBoxIcon. Exclamation );

Application.Exi t();
}

As I mentioned, I stepped through the code and even stepped through it
in disassembly mode to make sure the Exit function was actually being
called.

My workaround is to replace Application.Exi t() with:

throw new System.Exceptio n();

And then catch it in Main, and call Application.Exi t() there.

[STAThread]
static void Main()
{
try
{
Application.Run (new frmITQ());
}
catch
{
Application.Exi t();
}
}

Does anyone have a clue for me? (Yes, I know I should make a custom
exception and test specifically for it, but this is just a
troubleshooting technique and I haven't completely given up on
Application.Exi t() yet.)
-- Rick

Jul 21 '05 #2

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

Similar topics

1
5787
by: Dev | last post by:
Dear Friends, I have created Application using VC#. How do to the following tasks? The user should not be allowed to exit the program: the start menu, end task and all other ways of exiting the program should be disabled. The Shift+Ctrl+Q (or other keystroke combination) should be the only way to exit the program.
4
48042
by: Rajesh Abraham | last post by:
I have a form, which is getting some settings from the App.config in the form load. If the Key doesn't exist, I need to close the application after trapping the error. I tried Application.Exit() and Form.Close() neither seems to be the right one. Thanks, Rajesh Abraham Chacko
8
2580
by: Andrew Warren | last post by:
I'm trying to exit a Windows Forms application while in the form's constructor (after InitializeComponent() has been called) and am finding that calling Application.Exit () still leaves the form displayed and running. Trying to force the form to close with this.Close() does not work either. Any suggestions? Thanks.
3
5072
by: ZoombyWoof | last post by:
Hi. I'm pretty new to C# so this may seem like the dumbest question today, but I'm stuck. I have a small application, one Form that opens a connection to a database in its constructor. If the connection goes wrong I display a messagebox and I want the application to end after the user clicks OK. I cant make that happen..... If I use...
4
11145
by: Chuck | last post by:
Hello everybody, I need to abort execution during start up, while the constructor called by Application.Run is executing. If the database fails to connect during my application's startup I want to display a message (no problem here) and then abort the program. However, after the attached code executes I end up with my main form and a...
3
2908
by: Lumpierbritches | last post by:
I have an application my partner wrote that would allow an autoresponse to any Mapi compliant email that apparently in .Net won't, can someone assist me with fixing this? Here is the code: Utilities Module: Option Explicit Option Base 1
12
5842
by: JohnR | last post by:
I have narrowed a problem down to a simple example. A form with two buttons. One EXIT and one FBD. The exit button does an "END" to end the application. The FBD button does a FolderBrowserDialog and nothing else. When I start the application and hit EXIT everything works fine. However if it display the FBD box (even if I don't pick a...
1
391
by: Guinness Mann | last post by:
Pardon me if this is not the optimum newsgroup for this post, but it's the only .NET newsgroup I read and I'm certain someone here can help me. I have a C# program that checks for an error condition and if it finds it it notifies the user with a MessageBox and then on the next line of code (example in a minute) it calls Application.Exit(). ...
5
2307
by: hzgt9b | last post by:
I am having a curious problem... When 'Application.Exit()' command is executed in my program it does not end. I set a breakpoint in the 'Dispose' methid that never gets called (yes, I am in Debug mode) - what gives... this is certianly strange bahavior. The code that I am running is below, I added Stop statements to alert me when the...
0
7502
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...
0
7434
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...
0
7692
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. ...
0
7791
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5360
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...
0
5078
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...
0
3491
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...
1
1921
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
1045
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.