Solved

Close Main form in its constructor.

Posted on 2004-08-28
13
231 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 86

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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
LVL 86

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

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

749 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