473,507 Members | 2,451 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Exiting an application

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.
Nov 15 '05 #1
8 2577
Try using this:

YourFormName.Dispose();
YourFormName.Close();

Hope this helps! :)

Ann

"Andrew Warren" <an*******@discussions.microsoft.com> wrote in message
news:40****************************@phx.gbl...
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.

Nov 15 '05 #2
It sounds like you are doing some sort of check in the constructor and
aborting the app if it fails. If this is the case I can think of a couple
of ways to avoid the result you are experencing:

1) Preform the check in your main function before calling Application.Run().
Something like:

static void Main() // Not tested
{
...
if(<test_pass>)
{
Application.Run(new App());
}
else
{
// run any error code here, for example:
Application.Run(new ErrorMsgBox());
}
}

2) Set the form's visible property to false and preform you check using the
form's Load event.
Note: Due to a bug in the .NET framework your form will still flash on the
screen for half a second before closing on XP systems running the 1.0
version of the framework.

Hope this helps.

Dave
"Andrew Warren" <an*******@discussions.microsoft.com> wrote in message
news:40****************************@phx.gbl...
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.

Nov 15 '05 #3
Hello Ann!

Just as an FYI to everyone, Close methods should call dispose internally*
(so there shouldn't be a need to call them separately), and you shouldn't
call members of a class after calling the Dispose method.
Please don't take this as criticism against your post, I always try to
encourage everyone to participate and contribute :-)

-Rob Teixeira [MVP]

* if you look at the IL for the Form class, Close doesn't call Dispose
directly, it posts a close message to form, and when the form handles
WM_CLOSE, it calls Dispose.

"Ann Marinas" <an*********@xtra.co.nz> wrote in message
news:uB**************@tk2msftngp13.phx.gbl...
Try using this:

YourFormName.Dispose();
YourFormName.Close();

Hope this helps! :)

Ann

Nov 15 '05 #4
These work: Probably pretty much in order of my preferred methodology:

1) Separate the form creation and Application Run method and check a bool
variable, such as this:
private bool doExit = false;
...
static void Main()
{
Form1 f1 = new Form1(); // set doExit to true in constructor if
there's a problem.
if( !f1.doExit )
{
Application.Run( f1 );
}
}
...
2) Can you delay the exit until the Load event? You can't close an
Application until after the message pump has begun processing, which happens
after your main Form constructor.

3) Throw an exception in your constructor and catch it in Main()

Chris R.

"Andrew Warren" <an*******@discussions.microsoft.com> wrote in message
news:40****************************@phx.gbl...
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.

Nov 15 '05 #5
I think you are right. :)

Thank you so much for correcting me. I really do appreciate it! :)
Ann
"Rob Teixeira [MVP]" <RobTeixeira@@msn.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Hello Ann!

Just as an FYI to everyone, Close methods should call dispose internally*
(so there shouldn't be a need to call them separately), and you shouldn't
call members of a class after calling the Dispose method.
Please don't take this as criticism against your post, I always try to
encourage everyone to participate and contribute :-)

-Rob Teixeira [MVP]

* if you look at the IL for the Form class, Close doesn't call Dispose
directly, it posts a close message to form, and when the form handles
WM_CLOSE, it calls Dispose.

"Ann Marinas" <an*********@xtra.co.nz> wrote in message
news:uB**************@tk2msftngp13.phx.gbl...
Try using this:

YourFormName.Dispose();
YourFormName.Close();

Hope this helps! :)

Ann


Nov 15 '05 #6
Thanks Chris. I'd already coded a solution along the
lines of your first option but I couldn't understand why
Application.Exit() _didn't_! Your comment in suggestion
2 answers this to some extent but raises the question -
when does the message pump start processing exactly? From
your comments it's some time between exiting the
constructor and exiting form.Load().
Nov 15 '05 #7
Thanks Dave. I rearranged the code to operate along the
lines of your first suggestion.
Nov 15 '05 #8
Hello Andrew,

Actually, it's suggestion 1) that give the best clue, because the first
thing to run is the Form1 Constructor. Then the Application.Run is started,
beginning the message pump at this point, then the Form1.Load() is run.

Application.Run( new Form1() );
// Is equivalent to:
Form1 f1 = new Form1();
Application.Run( f1 );
// except this will hold a few extra bytes of memory.

When using
Application.Run( new Form1() );
the "new Form1()" is evaluated first, running the constructor and passing it
to the Application.Run as a parameter, and Run runs. It is after this that
the Load event is called. So, suggestion 1) and this way are really the
same thing, except suggestion 1) breaks it down the it's logical steps and
allows you to add intervening steps.

Chris R.

"Andrew Warren" <an*******@discussions.microsoft.com> wrote in message
news:02****************************@phx.gbl...
Thanks Chris. I'd already coded a solution along the
lines of your first option but I couldn't understand why
Application.Exit() _didn't_! Your comment in suggestion
2 answers this to some extent but raises the question -
when does the message pump start processing exactly? From
your comments it's some time between exiting the
constructor and exiting form.Load().

Nov 15 '05 #9

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

Similar topics

7
3967
by: David Elliott | last post by:
I have created an application that will dynamically load other DLLs (plugins). The new plugin is a winform with an embedded IE Browser. I am wanting to have the form to run in its own thread....
2
1743
by: pnp | last post by:
Hi all, I'm working on a win app that uses a logon form to verify the user that logs in the program and then hides the first form and displays an MDI form where the user does his work. The app is...
6
3665
by: GL | last post by:
I am getting a crash while exiting from my application. Pl find the Stack Trace of the crash: System.Windows.Forms.Application.ThreadWindows.ThreadWindows(System.Windows.Forms.Control parent =...
3
1504
by: Bonj | last post by:
When trying to debug an ASP.NET project by placing a breakpoint in one of the functions in the codebehind, VS correctly stops at the breakpoint, and I can step through. But soon after, the process...
2
10540
by: tt | last post by:
Hi, ANy ideas on this? When I click the close button of a window in VB.net which method gets called? I used Applicaton.exit in some method of my appln. to exit the application but, it still...
5
3844
by: Tim Werth | last post by:
I have a .NET console application that is kicked off by a .NET Windows service. They communicate via .NET Remoting, although there isn't much communication between the two while the console app is...
1
1892
by: kuhrty | last post by:
Hi, I am creating a multi-winform project. Everything has been working fine, except after I perform an update to the database and decide to Exit the winform after the update operation is...
6
2473
by: Ant | last post by:
Hi all, I'm putting together a simple help module for my applications, using html files stored in the application directory somewhere. Basically it sets up a basic web server, and then uses the...
5
10564
by: care02 | last post by:
Hi! I have the following problem: I have written a short Python server that creates an indefinite simulation thread that I want to kill when quitting (Ctrl-C) from Python. Googling around has...
0
7314
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7030
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...
0
7482
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...
0
5623
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,...
1
5041
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...
0
3191
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...
0
1540
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 ...
1
758
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
411
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...

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.