473,883 Members | 1,646 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 InitializeCompo nent() has
been called) and am finding that calling Application.Exi t
() 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 2603
Try using this:

YourFormName.Di spose();
YourFormName.Cl ose();

Hope this helps! :)

Ann

"Andrew Warren" <an*******@disc ussions.microso ft.com> wrote in message
news:40******** *************** *****@phx.gbl.. .
I'm trying to exit a Windows Forms application while in
the form's constructor (after InitializeCompo nent() has
been called) and am finding that calling Application.Exi t
() 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*******@disc ussions.microso ft.com> wrote in message
news:40******** *************** *****@phx.gbl.. .
I'm trying to exit a Windows Forms application while in
the form's constructor (after InitializeCompo nent() has
been called) and am finding that calling Application.Exi t
() 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*********@xt ra.co.nz> wrote in message
news:uB******** ******@tk2msftn gp13.phx.gbl...
Try using this:

YourFormName.Di spose();
YourFormName.Cl ose();

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*******@disc ussions.microso ft.com> wrote in message
news:40******** *************** *****@phx.gbl.. .
I'm trying to exit a Windows Forms application while in
the form's constructor (after InitializeCompo nent() has
been called) and am finding that calling Application.Exi t
() 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@@m sn.com> wrote in message
news:%2******** ********@TK2MSF TNGP11.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*********@xt ra.co.nz> wrote in message
news:uB******** ******@tk2msftn gp13.phx.gbl...
Try using this:

YourFormName.Di spose();
YourFormName.Cl ose();

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.Exi t() _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*******@disc ussions.microso ft.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.Exi t() _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
4008
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. This would allow for other plugins and the main application to be free to do other work. I have written a little TestDriver for the plugin and am having some difficulty. If I don't use threads everything works just fine. If I do use threads, upon...
2
1768
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 STA. The problem is that I wan't to be able to close the app from wherever in my app (no matter how deep in the code) and make sure that all the resources are being freed after exiting the app. Any ideas? Is there a way to re-open the app...
6
3688
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 = <undefined value>, bool onlyWinForms = true) + 0x8c bytes system.windows.forms.dll!ThreadContext.DisposeThreadWindows() + 0x54 bytes system.windows.forms.dll!ThreadContext.Dispose() + 0xa1 bytes...
3
1519
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 stops dead and can't be stepped through anymore (even though there is no error) - I'm guessing that the aspnet_wp.exe is detecting that if it's inactive for a period of n seconds or so, then it exits until it gets another request. Is there...
2
10566
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 leaves the process in the process table. Can someone help me exiting an application cleanly. Thanks -tt
5
3869
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 running. There can be, and are typically, several instances of the console app running at the same time communicating back to the service. The console app creates a delegate to do some work while the main thread waits on a ManualResetEvent to...
1
1920
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 complete. I have error handling in my code but not on the dispose method. I call the dispose method exiting the application using the code below but results in this error message "An unhandled exception of type 'System.NullReferenceException'
6
2524
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 webbrowser module to display it in the users browser. I have it set up to call sys.exit(0) if the url quit.html is called, but for some reason (on Windows XP via the cmd.exe shell) python won't let me have the console back. I get the System Exit...
5
10616
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 not given me any hints on how to cleanly kill running threads before exiting. Any help is appreciated! Carl
0
9932
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
10730
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10833
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
10405
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
9559
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
7959
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
5784
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
4205
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3228
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.