1

I realize after i published my program , that it didnt exit properly as the process still exist in task manager.

I've look into How do I properly close a winforms application in C#? and i found out that my Application.Run(new MainForm()); is my login form.

So my program runs like this.

After login form , it will lead to either AdminForm or UserForm.

So how do i do the exit program properly?

update

//this doesnt work//
private void UserForm_FormClosing(object sender, FormClosingEventArgs e)
{
    Application.Exit();
}

//==============//
private void User_Load(object sender, EventArgs e)
{
    some code i have..
}

Update 2 : I am currently editing this with Visual Studio 2012.

My program starts with this form SMECS

and after logging in either as admin or a user , they will be forwarded to this form. This is the admin form. The user form have the same template , but different naming and some buttons are disabled for the user. ASmecs

i just need to know how do i exit my program properly when i am in either AdminForm or UserForm..

Update 3 :

Login Form Code :

private void loginscs_Click(object sender, EventArgs e)
        {

            try
            {
                string userNameText = txtUser.Text;
                string passwordText = txtPass.Text;
                string isAdmin = "yes";
                string isNotAdmin = "no";
if (!(string.IsNullOrEmpty(txtUser.Text)) && !(string.IsNullOrEmpty(txtPass.Text)))
                {
                    SqlConnection SCScon = new SqlConnection();
                    SCScon.ConnectionString = "Data Source=PEWPEWDIEPIE\\SQLEXPRESS;Initial Catalog=master;Integrated Security=True";
                    SqlCommand cmd = new SqlCommand("SELECT ISNULL(SCSID, '') AS SCSID, ISNULL(SCSPass,'') AS SCSPass, ISNULL(isAdmin,'') AS isAdmin FROM SCSID WHERE SCSID='" + txtUser.Text + "' and SCSPass='" + txtPass.Text + "'", SCScon);
                    SCScon.Open();
                    SqlDataReader dr = cmd.ExecuteReader();
                    if (dr.Read())
                    {
                        if (this.CompareStrings(dr["SCSID"].ToString(), txtUser.Text) &&
                            this.CompareStrings(dr["SCSPass"].ToString(), txtPass.Text) &&
                            this.CompareStrings(dr["isAdmin"].ToString(), isAdmin))
                        {
                            MessageBox.Show("Hello " + txtUser.Text, "Admin", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            var adminf = new Admin(txtUser.Text);
                            this.Hide();
                            adminf.ShowDialog();
                        }
                        else if (this.CompareStrings(dr["SCSID"].ToString(), txtUser.Text) &&
                            this.CompareStrings(dr["SCSPass"].ToString(), txtPass.Text) &&
                            this.CompareStrings(dr["isAdmin"].ToString(), isNotAdmin))
                        {
                            MessageBox.Show(string.Format("Welcome {0}", txtUser.Text));
                            var userf = new UserForm(txtUser.Text);
                            this.Hide();
                            userf.ShowDialog();
                        }
                    }
                    else
                    {
                        MessageBox.Show("Wrong ID/Pass");
                    }
                    SCScon.Close();
                }

this is code from Program.cs(which is the one created by Visual Studio)

static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Login());
        }
    }
Community
  • 1
  • 1
  • How about showing your code? – L.B Dec 31 '13 at 12:51
  • @L.B -i have edited my question . i've tried the code.. but it didnt work. – user3149316 Dec 31 '13 at 12:54
  • How do you create/show your forms MainForm/AdminForm/UserForm (How do you use `Application.Run(..)`)? – L.B Dec 31 '13 at 12:56
  • @L.B - give me 1 minute to edit. – user3149316 Dec 31 '13 at 13:06
  • @L.B - i have edited my question. it is in Update 2. – user3149316 Dec 31 '13 at 13:15
  • 2
    Your update does not provide any new information that will help us give you a answer, we need to see ***code*** of how you open and close your forms, we don't care about how they look. – Scott Chamberlain Dec 31 '13 at 13:58
  • @ScottChamberlain - added the codes you needed – user3149316 Dec 31 '13 at 14:49
  • Do you start any new threads? If they are foreground sl threads they won't exit even if the app has called an exit. You need to end them or spin them up as background threads – devshorts Dec 31 '13 at 14:52
  • @user3149316 It seems you never close the `Login` form. You only hide it to show other forms. You have to close it to exit from your application. – L.B Dec 31 '13 at 14:52
  • possible duplicate of [How to close the current form and open another at the same time](http://stackoverflow.com/questions/12678071/how-to-close-the-current-form-and-open-another-at-the-same-time) – Servy Dec 31 '13 at 15:03
  • @Servy - dude.. that question was a year ago , by different people.. maigad.. – user3149316 Dec 31 '13 at 15:34
  • @user3149316 Sorry, copied the wrong tab's link by mistake. Meant to post this one: http://stackoverflow.com/q/20645476/1159478 – Servy Dec 31 '13 at 15:36
  • wow.. again.. that question... come on bro.. =.=" i just registered today , with only 1 post.. – user3149316 Dec 31 '13 at 16:09

3 Answers3

0

When a user closes your main form, call Application.Exit().

MSDN Link: http://msdn.microsoft.com/en-us/library/system.windows.forms.application.exit(v=vs.110).aspx

This method injects a WM_CLOSE message into all message queues.

Put it in your FormClosing event.

Simon Whitehead
  • 63,300
  • 9
  • 114
  • 138
0

You technically have 3 forums, not two. You close the "inner" form but you never close the "outer" form. The simplest way to fix it is just call Close after you show the dialog for the "inner" form.

if (dr.Read())
{
    if (this.CompareStrings(dr["SCSID"].ToString(), txtUser.Text) &&
        this.CompareStrings(dr["SCSPass"].ToString(), txtPass.Text) &&
        this.CompareStrings(dr["isAdmin"].ToString(), isAdmin))
    {
        MessageBox.Show("Hello " + txtUser.Text, "Admin", MessageBoxButtons.OK, MessageBoxIcon.Information);
        var adminf = new Admin(txtUser.Text);
        this.Hide();
        adminf.ShowDialog(); //This line blocks till you close the form
        this.Close(); // <--- NEW LINE
    }
    else if (this.CompareStrings(dr["SCSID"].ToString(), txtUser.Text) &&
        this.CompareStrings(dr["SCSPass"].ToString(), txtPass.Text) &&
        this.CompareStrings(dr["isAdmin"].ToString(), isNotAdmin))
    {
        MessageBox.Show(string.Format("Welcome {0}", txtUser.Text));
        var userf = new UserForm(txtUser.Text);
        this.Hide();
        userf.ShowDialog(); //This line blocks till you close the form
        this.Close(); // <--- NEW LINE
    }
}

On a side note, you really need to be using Parametrized Queries. If I decided to try to use the password '; drop table SCSID; -- in your login form can you tell me what would happen?

You also really should be using using statements around all objects that implement IDisposable. At a quick glance that would be SqlConnection, SqlCommand, and SqlReader but there may be more. Also if you are using a using statment you don't need to call SCScon.Close();

Community
  • 1
  • 1
Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
0

For your log in form, create an instance of the form and call the ShowModal method. After they have logged in, use Application.Run for either the user or admin form (based on whatever logic you're using to determine which form). Application.Run is a blocking call (the line after that statement will not run) until the form your passed in is closed by the user. After that, the code will continue to execute, and when there is no more code to execute, then the application will exit naturally (no need to explicitly call anything to close the application).

Brian Ball
  • 12,268
  • 3
  • 40
  • 51