Solved

Close Main form in its constructor.

Posted on 2004-08-28
13
227 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

746 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

16 Experts available now in Live!

Get 1:1 Help Now