Application.Exit() fails

  • Thread starter Thread starter Guinness Mann
  • Start date Start date
G

Guinness Mann

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
 
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 said:
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
 
Back
Top