1

I have made a simple form using C# on Visual Studio. I have exported my form as .exe and when I close the form by clicking the window close button, the application is still running in the task manager, even though the window itself is closed. (see screenshot below)

My program.cs

    static class Program
    {
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form2());
        }
    }

taskmanager

NimaJan
  • 162
  • 1
  • 13
  • Winforms or WPF? Show the code that launches your form (perhaps in Program.cs) – pinkfloydx33 Jul 05 '20 at 14:42
  • Do you use some threads or technologies and some controls or libraries using threads ? –  Jul 05 '20 at 15:08
  • 1
    A standard Winforms project closes when the form passed to `Application.Run()` is closed...unless you have created another thread and set its `IsBackground` property to false. Or you have written code that possibly HIDES the form instead of closing it? – Idle_Mind Jul 05 '20 at 16:27
  • Is `Form2` the form you are closing? Do you open any other forms? – pinkfloydx33 Jul 05 '20 at 22:28
  • @pinkfloydx33 I also open form1 from form2. – NimaJan Jul 06 '20 at 03:13
  • Which form is it that you are closing when the application doesn't automatically exit like you expect it to? Is it form1 or Form2? What happens to Form2 when you open form1? Can you update the question to show *how* you are opening that form? I know you got an answer below but I'd like to see you get one that makes everything behave like it's expected to – pinkfloydx33 Jul 06 '20 at 08:23

2 Answers2

1

You can call Application.Exit() when you want to close down your application.

That will close the windows and if it was the last and the application first window was started with Application.Run(new Form()) it should also end the whole application. However it seems there is a window that is not closed but hidden and/or it maybe was started with Application.Run() or there is something else keeping it from closing the whole application.

Usually program.cs contains how it is all started.

  • There is `Environment.Exit()` too that is more radical. –  Jul 05 '20 at 15:07
  • @Vivian I added `Application.Exit()` in my FormClosed event and it works perfectly. Many thanks. – NimaJan Jul 05 '20 at 19:20
  • @OlivierRogier I get the following error when I try `Environment.Exit()`: Error CS7036 There is no argument given that corresponds to the required formal parameter 'exitCode' of 'Environment.Exit(int)' vervalDatum. – NimaJan Jul 05 '20 at 19:21
  • Yes, this method requires the use of the execution result or *exit code* (inherited from DOS)... You can try `Environment.Exit(0);`. See https://learn.microsoft.com/dotnet/api/system.environment.exit for more details. –  Jul 05 '20 at 19:36
  • 1
    While this may work, it is absolutely positively the wrong answer. If you are forced to rely on manually calling Application.Exit in your form closed event then it means you are doing something wrong *somewhere*. All Winforms/WPF applicationz will automatically terminate when the main form is closed making this absolutely unnecessary. This is why I asked in a comment how you were creating the form and to show your program.cs/entry point – pinkfloydx33 Jul 05 '20 at 19:49
  • @pinkfloydx33 I indeed misunderstood your statement. I have added my program.cs to my question, maybe you can see the mistake. Thanks in advance. – NimaJan Jul 05 '20 at 21:33
0

In form closing event kill the process.

var chromeDriverProcesses = Process.GetProcesses().
    Where(pr => pr.ProcessName == "Exefilename"); // without '.exe'

foreach (var process in chromeDriverProcesses)
{
     process.Kill();
}
LDS
  • 354
  • 3
  • 9
  • 1
    Definitely not the solution, but if it *were* then `Process.GetCurrentProcess()` would be a better answer – pinkfloydx33 Jul 05 '20 at 14:41
  • @pinkfloydx33 his code actually works, but `Application.Exit()` is much more clear. – NimaJan Jul 05 '20 at 19:24
  • @NimaJan I was mostly remaking on the fact the LINQ was unnecessary, but also on process. Kill. While they may 'work' neither Application.Exit nor Process.Kill are the correct answer to your problem. All Winforms/WPF apps terminate automatically once the main form is closed. If that's not happening it means you are either doing something incorrect somewhere or perhaps have a (now) hidden main form from which you created an instance of the form you are closing. You are circumventing the application life-cycle, which is why I asked you to share some code and clarify in your original question – pinkfloydx33 Jul 05 '20 at 19:55