472,127 Members | 1,482 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,127 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 8329
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Rajesh Abraham | last post: by
8 posts views Thread by Andrew Warren | last post: by
3 posts views Thread by ZoombyWoof | last post: by
4 posts views Thread by Chuck | last post: by
3 posts views Thread by Lumpierbritches | last post: by
1 post views Thread by Guinness Mann | last post: by
5 posts views Thread by hzgt9b | last post: by

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.