473,542 Members | 2,045 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 8489
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
2579
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
2907
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
5837
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
2306
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
7330
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
7721
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...
1
7324
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...
0
7670
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...
0
5867
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...
0
3380
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...
0
3376
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1798
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
943
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.