Solved

how to exit the application before the form has been initialized?

Posted on 2011-03-21
23
536 Views
Last Modified: 2013-12-17
hi,

how to exit the application before the form has been initialized?
here is my code that is not working:

        public UserAuth()
        {
            Splach s = new Splach();
            s.ShowDialog();
            if (!s.isDriverMapped)
            {
                DevComponents.DotNetBar.MessageBoxEx.Show("Failed to connect to server,\nThe software will be terminated...", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Application.ExitThread();
                Application.Exit();
            }

            if (!DataAccess.ConnectionManager.isDBLive())
            {
                superValidator1.Enabled = false;
                DevComponents.DotNetBar.MessageBoxEx.Show("Failed to connect to server,\nThe software will be terminated...", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                Application.ExitThread();
                Application.Exit();
            }
            InitializeComponent();
        }
0
Comment
Question by:njgroup
  • 9
  • 7
  • 3
  • +4
23 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 35179167
And what is not working ? What is the issue ?
Can you show the main-thread of yorur application ... The procedure where you start with, where you call UserAuth...
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 35179227
public UserAuth()
        {
            Splach s = new Splach();
            s.ShowDialog();
            if (!s.isDriverMapped)
            {
                DevComponents.DotNetBar.MessageBoxEx.Show("Failed to connect to server,\nThe software will be terminated...", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Application.ExitThread();
                Application.Exit();
return;
            }

            if (!DataAccess.ConnectionManager.isDBLive())
            {
                superValidator1.Enabled = false;
                DevComponents.DotNetBar.MessageBoxEx.Show("Failed to connect to server,\nThe software will be terminated...", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                Application.ExitThread();
                Application.Exit();
return;
            }
            InitializeComponent();
        }
0
 
LVL 10

Expert Comment

by:John Claes
ID: 35179377
U can make sure the Initialize components can only be calle when both previous tests are ok


1) Get way : using return to get out when test is not valid : see code AndyAinscow:
2) Testing : instead of using returns u can also implement the If's using else branches (see example)

in both cases the initialisation is only called when both tests have succeeded

public UserAuth()
        {
            Splach s = new Splach();
            s.ShowDialog();
            if (!s.isDriverMapped)
            {
                DevComponents.DotNetBar.MessageBoxEx.Show("Failed to connect to server,\nThe software will be terminated...", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Application.ExitThread();
                Application.Exit();
            }
            else if (!DataAccess.ConnectionManager.isDBLive())
            {
                superValidator1.Enabled = false;
                DevComponents.DotNetBar.MessageBoxEx.Show("Failed to connect to server,\nThe software will be terminated...", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                Application.ExitThread();
                Application.Exit();
            }
            else
            {
            InitializeComponent();
            }
        }

regards

poor beggar

0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 35181122
You could also move the "meat" of that code to program.cs and only call Application.Run() if your tests pass.
0
 
LVL 1

Expert Comment

by:menxin
ID: 35186121
Yes,Idle_Mind is right. Ican give you an example:

In your Program.cs:
    static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Splach s = new Splach();
            s.ShowDialog();
            if (!s.isDriverMapped)
            {
                DevComponents.DotNetBar.MessageBoxEx.Show("Failed to connect to server,\nThe software will be terminated...", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Application.ExitThread();
                Application.Exit();
            }
            else if (!DataAccess.ConnectionManager.isDBLive())
            {
                superValidator1.Enabled = false;
                DevComponents.DotNetBar.MessageBoxEx.Show("Failed to connect to server,\nThe software will be terminated...", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
                Application.Run(new MainForm());
        }
    }

Open in new window

0
 
LVL 5

Expert Comment

by:samml
ID: 35194428
It's not really elegant so this should not be your first choice.
System.Diagnostics.Process.GetCurrentProcess().Kill()

Open in new window

It does the same as if you would select your programm in the task manager and click kill.
0
 

Author Comment

by:njgroup
ID: 35224792
return is not destroying the application, its continue running with empty form
0
 

Author Comment

by:njgroup
ID: 35224800
can you please try the "return;"
because it gives me empty form and the program is not closed!
0
 
LVL 1

Accepted Solution

by:
menxin earned 350 total points
ID: 35224891
Your Splach object is not destroyed? The Main process only create a splach object and if the auth test failed,it will do nothing.You can trace the code to see why the application cant quit?

static class Program  
    {  
        [STAThread]  
        static void Main()  
        {  
            Application.EnableVisualStyles();  
            Application.SetCompatibleTextRenderingDefault(false);  
            Splach s = new Splach();  
            s.ShowDialog();  
            if (!s.isDriverMapped)  
            {  
                DevComponents.DotNetBar.MessageBoxEx.Show("Failed to connect to server,\nThe software will be terminated...", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);  
            }  
            else if (!DataAccess.ConnectionManager.isDBLive())  
            {  
                superValidator1.Enabled = false;  
                DevComponents.DotNetBar.MessageBoxEx.Show("Failed to connect to server,\nThe software will be terminated...", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);  
            }  
            else  
                Application.Run(new MainForm());  
        }  
    }

Open in new window

0
 

Author Comment

by:njgroup
ID: 35225558
I have disposed the Splach object but that does not work too

                s.Dispose();
                GC.WaitForPendingFinalizers();
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 35225559
>>return is not destroying the application, its continue running with empty form

That should not happen, please post your code.
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:njgroup
ID: 35225792
public UserAuth()
        {
            Splach s = new Splach();
            s.ShowDialog();
            if (true)
            {
                DevComponents.DotNetBar.MessageBoxEx.Show("Failed to connect to server,\nThe software will be terminated...", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                s.Dispose();
                GC.WaitForPendingFinalizers();
                Application.ExitThread();
                Application.Exit();
                return;
            }

            if (!DataAccess.ConnectionManager.isDBLive())
            {
                superValidator1.Enabled = false;
                DevComponents.DotNetBar.MessageBoxEx.Show("Failed to connect to server,\nThe software will be terminated...", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                Application.ExitThread();
                Application.Exit();
                return;
            }
            InitializeComponent();
        }
0
 

Author Comment

by:njgroup
ID: 35225861
can you try it in new project?
I created new project and I put return just before InitializeComponent(); but same problem occurred!

so what to do?
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 35225867
Thanks, but not quite what I wanted.
Where/how do you call UserAuth ?
0
 

Author Comment

by:njgroup
ID: 35225868
maybe because it is inherited from System.Windows.Forms.Form, isn't it?
0
 

Author Comment

by:njgroup
ID: 35225869
   static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            try
            {
                Application.Run(new UserAuth());
            }
            catch(Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
0
 

Author Comment

by:njgroup
ID: 35225876
in program.cs I have main:
   static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            try
            {
                Application.Run(new UserAuth());
            }
            catch(Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }

0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 35225886
Try this

public UserAuth()
        {
            Splach s = new Splach();
            s.ShowDialog();
            if (true)
            {
                DevComponents.DotNetBar.MessageBoxEx.Show("Failed to connect to server,\nThe software will be terminated...", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (!DataAccess.ConnectionManager.isDBLive())
            {
                superValidator1.Enabled = false;
                DevComponents.DotNetBar.MessageBoxEx.Show("Failed to connect to server,\nThe software will be terminated...", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            InitializeComponent();
        }

Open in new window

0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 35225893
Oops sorry, try this

public UserAuth()
        {
            Splach s = new Splach();
            s.ShowDialog();
            if (true)
            {
                DevComponents.DotNetBar.MessageBoxEx.Show("Failed to connect to server,\nThe software will be terminated...", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

this.Close();
                return;
            }

            if (!DataAccess.ConnectionManager.isDBLive())
            {
                superValidator1.Enabled = false;
                DevComponents.DotNetBar.MessageBoxEx.Show("Failed to connect to server,\nThe software will be terminated...", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

this.Close();
                return;
            }
            InitializeComponent();
        }

Open in new window

0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 35225897
An application should stop when the main form is closed, you should not need to use Application.Exit or Application.ExitThread under normal circumstances.  They are really intended for emergency aborting of the application/thread.
0
 

Author Comment

by:njgroup
ID: 35225925
on Application.Run(new Form1()); and after execution to this commands:
this.Close();
return;

I got this exception:

Cannot access a disposed object.
Object name: 'UserAuth'.

here is snapshot:

 exception
0
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 150 total points
ID: 35225983
Hmmm.
I can duplicate the behaviour.  Trying to close it in the constructor is making the .net environment unhappy - it is attempting to perform further tasks with the newly created object (which you have immediately destroyed) hence the exception/blank form you experience.


It means you need a (small) change to the logic.
You can implement the suggestion of Idle_Mind to perform your checks and splash screen in the 'main' section and only show this form if the checks are successful.
OR
You could move these checks to the Shown event of the (UserAuth) form - it really depends on how you want your app to appear to the user
0
 
LVL 1

Expert Comment

by:menxin
ID: 35253046
Your problem is you want auth user in construct function.
...,If i was you ,i will change the code to:
1.
static class Program    
    {    
        [STAThread]    
        static void Main()    
        {    
            Application.EnableVisualStyles();    
            Application.SetCompatibleTextRenderingDefault(false);    
            Splach s = new Splach();    
            s.ShowDialog();    
            if (!s.isDriverMapped)    
            {    
                DevComponents.DotNetBar.MessageBoxEx.Show("Failed to connect to server,\nThe software will be terminated...", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);    
            }    
            else if (!DataAccess.ConnectionManager.isDBLive())    
            {    
                superValidator1.Enabled = false;    
                DevComponents.DotNetBar.MessageBoxEx.Show("Failed to connect to server,\nThe software will be terminated...", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);    
            }    
            else  if((new UserAuth()).ShowDialog == DialogResult.OK)
                Application.Run(new MainForm());  
        }    
    } 

Open in new window


and your UserAuth() is Changed to:
public UserAuth()  
        {  
            InitializeComponent();  
        }

Open in new window


2.another way:
        //make a canclose flag value
        bool canclose = false;
        
        public UserAuth()
        {
   Splach s = new Splach();
            s.ShowDialog();
            if (!s.isDriverMapped)
            {
                DevComponents.DotNetBar.MessageBoxEx.Show("Failed to connect to server,\nThe software will be terminated...", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.canclose =true;
            }
            else if (!DataAccess.ConnectionManager.isDBLive())
            {
                superValidator1.Enabled = false;
                DevComponents.DotNetBar.MessageBoxEx.Show("Failed to connect to server,\nThe software will be terminated...", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.canclose =true;
            }
            InitializeComponent();
        } 
....
        private void UserAuth_Load(object sender, EventArgs e)
        {
           if(canclose)
                    this.Close();
        }

Open in new window

0

Featured Post

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!

Join & Write a Comment

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
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 video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

758 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

20 Experts available now in Live!

Get 1:1 Help Now