Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

close an windows app

Posted on 2011-03-25
6
Medium Priority
?
494 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 2000 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
Technology Partners: 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!

 

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

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

722 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