Search code examples
c#winformsvisual-studio-2013background-process

Windows forms kill background process on app exit


I am making a simple windows form application in Visual Studio. But I have a problem when I want to close the app. When I right-click it from taskbar and close it, I can see background process still running in Task Manager.
How do I kill whole app?
I have paced this in first form that is opening when app starts:

     private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if(!exit)
            {
                if (MessageBox.Show("Close the app?", "Simple App", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    exit = true;
                    Close();
                }
                else
                {
                    e.Cancel = true;
                    exit = false;
                }
            }
        }

Solution

  • I added Application.Exit(); on all forms in OnFormClosing event. And it is working good.