Solved

Close Main form in its constructor.

Posted on 2004-08-28
13
230 Views
Last Modified: 2008-02-07
I have a MainForm acting as the starting form of my app.  In Main(), it simply look like
static void Main()
{
 try
 {
  Application.Run(new FormMain());
 }
catch(Exception exc)
{
  MessageBox.Show(exc.Message);
}
}


In the constructor of FormMain, it look like:
public FormMain0()
{
//-----Because of some case, I need to exit my application here----
if(booNeedToExit)
{
  Application.Exit();
  return;
}

InitializeComponent();            // This call is required by the Windows Form Designer.
}

Why a small empty windows appears(my application doen't exit) when booNeedToExit=true?
How can I exit my application and release all resources in this case?
Thanks.
0
Comment
Question by:kennon2000
  • 3
  • 3
  • 2
  • +2
13 Comments
 
LVL 19

Expert Comment

by:drichards
ID: 11923364
That won't work because Application.Run() has not yet been called, so there is no application o Exit yet.  The form is constructed as a parameter to Application.Run().  To do what you want, set a member variable in Form1 - you can just make 'boolNeedToExit' a member of Form1.  Then find an event that happens later, like Load, and add a handler for that.  In the handler, you can do:

      private void Form1_Load(object sender, System.EventArgs e)
      {
            if ( boolNeedToExit) Application.Exit();
      }
0
 
LVL 19

Accepted Solution

by:
drichards earned 34 total points
ID: 11923369
You still set 'boolNeedToExit' in the constructor like you were before if that's where your logic is to determine its value.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 11923376
Do your check from within the Form Load event.  I don't think you can kill a form from within its constructor...

It would look like this in VB:

    Private booNeedToExit As Boolean

    Private Sub FormMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       
        ' -----Because of some case, I need to exit my application here----
        booNeedToExit = True
        If booNeedToExit Then
            Me.Close()
        End If
    End Sub

Regards,

Idle_Mind
0
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 11923385
Oops, looks like I crossposted with drichards...who has already given you the same advice.

Idle_Mind
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11923433
why not just move the logic out of the constructor and into your main function ... never call te constructor if you need to exit (should be a bit faster)
0
 
LVL 19

Expert Comment

by:drichards
ID: 11923482
The assumption is that the logic to determine whether app should close is in Form1.  Otherwise, yes, just do it in the Main function.
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11923505
like I said ... move the logic :)
0
 

Author Comment

by:kennon2000
ID: 11987625
drichards is right, my logic to determine when my app quit is in FormMain( Form1 in drichards case), quite difficult to move to Main() because the decision is actually in another form called by FormMain.
Please give me a brief explaination for: When does a FormMain be called to show? Why it still be shown even I use return before InitializeComponent()?

Thanks everybody, you r really helpful.
0
 
LVL 37

Assisted Solution

by:gregoryyoung
gregoryyoung earned 33 total points
ID: 11990983
have the other form call application.exit()
0
 
LVL 5

Assisted Solution

by:ajitanand
ajitanand earned 33 total points
ID: 12109779
hello,

First thing - You can NEVER (yes never) unload a form in its constructor!!! (without getting an error!)

I had a similar thing in my app, that checks if a user is authorised to view the form or not. The code had to be placed in the form class itself. (as I was using generic code and Inheritance to achieve this in all my application forms).

So I had to do this:

Step 1. Added a Timer control on the form class. This timer had a very small interval (10 milliseconds!, hard to notice)
Step 2. Added the code to check and close the form in the Timer's Event.

And this worked perfectly well. I know this is a hack, but it works and in a better way than, having the application receive the event to close it from some other form or menu etc.


hope this helps.

rgds,
Ajit Anand



0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question