Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

close an windows app

Posted on 2011-03-25
6
Medium Priority
?
497 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 2000 total points
ID: 35214885
0
 
LVL 45

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
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!

 

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 45

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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Screencast - Getting to Know the Pipeline

972 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