Solved

close an windows app

Posted on 2011-03-25
6
482 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

752 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