473,897 Members | 2,656 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 8529
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
5803
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
48059
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
2604
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
5090
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 Application.Exit in my constructor the form still lives, if I do a this.dispose the program crashes and...
4
11179
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 wait cursor! If I click on the X the form closes. Boss doesn't think an unhandled exception will...
3
2931
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
5889
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 folder and immediately hit cancel) and then try to exit one of two things happens. Either (1) the...
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(). To my astonishment, I stepped through the code with the debugger, and watched it call...
5
2331
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 applicaiton.exit() fails to quit the app... Anyone have any ideas - I'm stuck!
0
9990
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9837
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10941
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
10478
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9656
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8032
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7185
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6074
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3300
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.