[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 566
  • Last Modified:

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

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
njgroup
Asked:
njgroup
  • 9
  • 7
  • 3
  • +4
2 Solutions
 
DhaestCommented:
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
 
AndyAinscowCommented:
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
 
John ClaesCommented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You could also move the "meat" of that code to program.cs and only call Application.Run() if your tests pass.
0
 
men xinCEOCommented:
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
 
sammlCommented:
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
 
njgroupAuthor Commented:
return is not destroying the application, its continue running with empty form
0
 
njgroupAuthor Commented:
can you please try the "return;"
because it gives me empty form and the program is not closed!
0
 
men xinCEOCommented:
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
 
njgroupAuthor Commented:
I have disposed the Splach object but that does not work too

                s.Dispose();
                GC.WaitForPendingFinalizers();
0
 
AndyAinscowCommented:
>>return is not destroying the application, its continue running with empty form

That should not happen, please post your code.
0
 
njgroupAuthor Commented:
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
 
njgroupAuthor Commented:
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
 
AndyAinscowCommented:
Thanks, but not quite what I wanted.
Where/how do you call UserAuth ?
0
 
njgroupAuthor Commented:
maybe because it is inherited from System.Windows.Forms.Form, isn't it?
0
 
njgroupAuthor Commented:
   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
 
njgroupAuthor Commented:
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
 
AndyAinscowCommented:
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
 
AndyAinscowCommented:
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
 
AndyAinscowCommented:
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
 
njgroupAuthor Commented:
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
 
AndyAinscowCommented:
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
 
men xinCEOCommented:
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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

  • 9
  • 7
  • 3
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now