WPF application still running in task manager even after I close out my application

HI Experts,

I hava WPF application using C#.  The application queries sql server database and it works fine. When I close out my main application interface window, when i look at the processes running in task manager it is still running even after i close my application.

To close my main interface window I have a button that when clicked calls the close method


        private void button3_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

So when I click the button the program closes but it is still running in task manager.

Does anyone know the way to close my application so it is not showing in the task manager after i close my program.

Thanks.
LVL 1
maqskywalkerAsked:
Who is Participating?
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
It's still running because you only hid the main window.

Try this instead:
                if (SQLReader["UNIT"].ToString() != "RECORDS")
                {
                    MessageBox.Show("Sorry, but you must be assigned to records before you can use this system.");
                }
                else
                {
                    //****Allow user access.
                    this.Hide(); // hide the main window

                    //****Open the main interface window.
                    Window2 Window2 = new Window2();
                    // note that I'm using ShowDialog() instead of Show()...
                    // you could also subscribe to the FormClosed() event instead and use "this.Close()" from there
                    Window2.ShowDialog(); // <------ code STOPS here until "Window2" is closed!

                    //****Close this form.
                    this.Close();

                    // now the app should exit properly
                }

Open in new window

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Do you have any polling loops or other threads in your application?
0
 
williamcampbellCommented:
Try Application.Exit () or Perhaps you didn't close your SQL Connecrtion
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
maqskywalkerAuthor Commented:
Basically this is what's going on.

I have a WPF window called Window1.xaml and the code behind is Window1.xaml.cs

On this page I have 2 textboxes, one where the user enters the User ID and the other they enter Password.

Then they click a button to login.

After they click the button their user name and password are checked for those values in sql server, if it is a valid user id and password then they are taken to the main application interface window which in my application is called Window2.xaml
So when the a person logins in, Window1.xaml closes and the main interface Window2.xaml opens.
After I'm done using Window2.xaml and I close that window. That is when I notice that the application is still running in the task manager.
Here is the code for Window1.xaml.cs which is the code for my login Window.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data.OleDb;

namespace LoginApp1
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void btnLogIn_Click(object sender, RoutedEventArgs e)
        {
            //****Access the database and check for a user i.d. and password match.

            //****Create a connection to SQL Server.
            String SQLConnectionString = "PROVIDER=SQLOLEDB;DATA SOURCE=VenusDB;USER ID=MyDBUserId;PASSWORD=MyDBPassword;";
            OleDbConnection SQLPrimaryConnection = new OleDbConnection(SQLConnectionString);

            //****Open the connction.
            SQLPrimaryConnection.Open();

            //****Create a command. Pull data from the VenusDB system.
            string mySelectQuery = "SELECT * FROM VenusDB.DBO.IDENTIFICATION INNER JOIN VenusDB.DBO.PERSON_EMPLOYEE ON VenusDB.DBO.IDENTIFICATION.PERSON_KEY = VenusDB.DBO.PERSON_EMPLOYEE.PERSON_KEY WHERE VenusDB.DBO.PERSON_EMPLOYEE.HISTORICAL_RECORD_FLAG = 0 AND VenusDB.DBO.PERSON_EMPLOYEE.DEACTIVATED_FLAG = 0 AND VenusDB.DBO.PERSON_EMPLOYEE.PASSWORD = '" + txtPassword.Text.ToUpper() + "' AND VenusDB.DBO.IDENTIFICATION.IDENTIFICATION_CODE = 'VenusDB SECONDARY USER ID' AND VenusDB.DBO.IDENTIFICATION.IDENTIFICATION_NUMBER = '" + txtUser_ID.Text.ToUpper() + "'";


            OleDbCommand SQLCommand = new OleDbCommand(mySelectQuery, SQLPrimaryConnection);

            //****read the results of the command.
            OleDbDataReader SQLReader = SQLCommand.ExecuteReader();
            SQLReader.Read();

            try
            {

                //****This next line is used for testing.
                //MessageBox.Show(SQLReader["PERSON_KEY"].ToString());

                if (SQLReader["UNIT"].ToString() != "RECORDS")
                {
                    MessageBox.Show("Sorry, but you must be assigned to records before you can use this system.");
                }
                else
                {

                    //****Allow user access.

                    //****Open the main interface window.
                    Window2 Window2 = new Window2();
                    Window2.Show();

                    //****Close this form.
                    //frmLogIn newform = (frmLogIn)Application.OpenForms["frmLogIn"];
                    this.Hide();
                    //frmLogIn.ActiveForm.Close();
                }
            }
            catch
            {
                MessageBox.Show("Sorry, but you did not enter a valid user I.D. or password.");
            }

            //****Close the connection.
            SQLPrimaryConnection.Close();

        }

    }
}

Open in new window

0
 
williamcampbellCommented:
The posters  original Question has this.Close ();

So I assume he tried it and it didn't work ...
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
It's possible that the Close() code in the original description was in the SECONDARY form?...
0
 
maqskywalkerAuthor Commented:
Idle_Man your the man!
0
 
maqskywalkerAuthor Commented:
I tried the suggestion that IDLE Mind gave in his code and that worked.

I changed this.Hide();
to this.Close();

and that worked.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.