Solved

Close Main form in its constructor.

Posted on 2004-08-28
13
228 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
 
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

914 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now