Solved

close an windows app

Posted on 2011-03-25
6
435 Views
Last Modified: 2012-05-11
to start , i am pretty new at the c# windows app world

when my app starts in the constructor of the form1 i check to see if two variables are populated)
(uname and password)

the problem i am having is if the user closes the login form or cancels i need the app to go away
i thought in the else of the dialogresult i could do
                   System.Windows.Forms.Application.Exit();

but that does not work.

looks for suggestions on the best way to do this?
thanks




       public Form1()
        {
            InitializeComponent();
            if (username == null || passwd == null)
            {
                DialogResult dr = new DialogResult();
                LoginForm login = new LoginForm();
                dr = login.ShowDialog();
                if (dr == DialogResult.OK)
                {
                }
                else
                {
                }
            }
        }

0
Comment
Question by:paries
6 Comments
 
LVL 6

Accepted Solution

by:
kswathi earned 500 total points
ID: 35214885
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 35214909
Is the following what you want?  Closing the main form of an app will close the app automatically.


 public Form1()
        {
            InitializeComponent();
            if (username == null || passwd == null)
            {
                DialogResult dr = new DialogResult();
                LoginForm login = new LoginForm();
                dr = login.ShowDialog();
                if (dr == DialogResult.OK)
                {
                }
                else
                {
this.Close();  //Close this form
                }
            }
        }
0
 

Author Comment

by:paries
ID: 35214967
Andy
i tried the this.Close();
it threw the exception  ::


System.InvalidOperationException was unhandled
  Message=Starting a second message loop on a single thread is not a valid operation. Use Form.ShowDialog instead.
  Source=System.Windows.Forms
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:paries
ID: 35214981
This works..
Use Environment.Exit(0)
thanks, found from kswathi:
0
 
LVL 7

Expert Comment

by:mkobrin
ID: 35214983
If you are going to use this you must remember that any code in the block after this.close(); will still execute, so you will need to put a return; afterwards to leave this block of code immiediatly:

public Form1()
        {
            InitializeComponent();
            if (username == null || passwd == null)
            {
                DialogResult dr = new DialogResult();
                LoginForm login = new LoginForm();
                dr = login.ShowDialog();
                if (dr == DialogResult.OK)
                {
                }
                else
                {
this.Close();  //Close this form
return; //Break out of this code block and force the close to happen immiediatly
                }
            }
        }
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 35215179
Exception - wierd.  You must have some more code that I wasn't expecting based on your question.
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

Suggested Solutions

Title # Comments Views Activity
Convert VB6 MSXML2.ServerXMLHTTP process to C# 2 36
getting id from database 5 24
Check my code out(2) 1 35
Create XML 5 34
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video discusses moving either the default database or any database to a new volume.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

747 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

8 Experts available now in Live!

Get 1:1 Help Now