?
Solved

Close Main form in its constructor.

Posted on 2004-08-28
13
Medium Priority
?
233 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 136 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
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 132 total points
ID: 11990983
have the other form call application.exit()
0
 
LVL 5

Assisted Solution

by:ajitanand
ajitanand earned 132 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

Interactive Way of Training for the AWS CSA Exam

An interactive way of learning that will help you visualize core concepts so that you can be more effective when taking your AWS certification exam.  Built for students by a student to help them understand the concepts that they are being taught.

Question has a verified solution.

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

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

752 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