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

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

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

Here's the code:

string s = "Some error message..."

if (!lNames.isValidLanguage(ref strLanguage))
{
MessageBox.Show (s, "AppName", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation );

Application.Exit();
}

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

throw new System.Exception();

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

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

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.Exit() yet.)
-- Rick
Jul 21 '05 #1
1 8467
lol. Most people have problems getting their programs to run rather than
exit ;-)

Here's an extract from the help about the Application.Exit() 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.Exit 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.Exit
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.exit and all cleanup has finished

End Sub

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

' Do rest of initialization processing
End Sub
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"Guinness Mann" <GM***@dublin.com> 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.Exit().

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

Here's the code:

string s = "Some error message..."

if (!lNames.isValidLanguage(ref strLanguage))
{
MessageBox.Show (s, "AppName", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation );

Application.Exit();
}

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

throw new System.Exception();

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

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

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

Jul 21 '05 #2

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

Similar topics

1
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...
4
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()...
8
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...
3
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...
4
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...
3
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: ...
12
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...
1
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...
5
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work

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.