Improve company productivity with a Business Account.Sign Up

x
?
Solved

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

Posted on 2011-03-21
23
Medium Priority
?
569 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 45

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
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
LVL 86

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:men xin
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:
men xin earned 1400 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 45

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
 

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 45

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 45

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 45

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 45

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 45

Assisted Solution

by:AndyAinscow
AndyAinscow earned 600 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:men xin
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Watch the working video to know how to import Outlook PST/OST files to Amazon WorkMail. Kernel released this tool which is very easy to use and migrate single or multiple PST and OST files to Amazon WorkMail. To know more about Kernel Import PST to …
A query can call a function, and a function can call Excel, even though we are in Access. This is Part 2, and steps you through the VBA that "wraps" Excel functionality so we can use its worksheet functions in Access. The declaration statement de…

588 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