Solved

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

Posted on 2011-03-21
23
538 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
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.

932 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

10 Experts available now in Live!

Get 1:1 Help Now