0

no matter what I tried, Visual Studio doesn't stop debugging even after closing the form.

using System;
using System.Windows.Forms;

namespace Database_Form
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.ApplicationExit += new EventHandler(Application_ApplicationExit);
            Application.Run(new LPEE());
        }

        static void Application_ApplicationExit(object sender, EventArgs e)
        {
            // Close all open forms
            foreach (Form form in Application.OpenForms)
            {
                form.Close();
            }
        }
    }
}

this is what I found online and wrote it in Program.cs, but it didn't work

JHBonarius
  • 10,824
  • 3
  • 22
  • 41
  • 1
    The code is wrong. Don't try random things. If the `LPEE` form really closes, the application will exit. That `Application_ApplicationExit` handler will try to close any open forms, something wasteful because these will be closed anyway. It won't fire at all unless the application is already in the process of closing – Panagiotis Kanavos Jul 17 '23 at 11:18
  • What does *your* code look like? Which form did you try to close and how? Did you really close it or hide it? – Panagiotis Kanavos Jul 17 '23 at 11:18
  • 2
    When running the application OUSIDE the debugger, does the process exit when you close the main window? Or does it leave a process running that's visible in Task Manager somewhere? – Matthew Watson Jul 17 '23 at 11:20
  • @PanagiotisKanavos i just realized a mistake, if i close the splash screen (the first form) it does end the debug, trying to close other forms doesn't end it – Vergil_The_Killer Jul 17 '23 at 11:21
  • @MatthewWatson it stays hidden in the background – Vergil_The_Killer Jul 17 '23 at 11:22
  • Post your code. WinForms is over 20 years old and everyone used splash screens at one point or another. Somehow, you aren't closing the main form but another one. – Panagiotis Kanavos Jul 17 '23 at 11:25
  • @PanagiotisKanavos which one specifically? because when i tried to close the first form (splash screen) it ended the debug – Vergil_The_Killer Jul 17 '23 at 11:34
  • i fixed it, but the solution is not really the best, since i had to keep adding Application.Exit(); in every form's FormClosed handeler – Vergil_The_Killer Jul 17 '23 at 11:44
  • There's no `first form`. There's the *main* form, in this case `LPEE` and child forms created by it. We can't guess how you created and displayed the splash screen, how you hide it or how you display what you consider the application's main form. – Panagiotis Kanavos Jul 17 '23 at 11:44
  • 1
    `i had to keep adding Application.Exit()` that's a hack that covers up the bug, not the fix. I suspect LPEE is the "splash screen" that gets hidden so close signals never get to it. That's a bug in the splash screen implementation – Panagiotis Kanavos Jul 17 '23 at 11:45
  • If you want to keep things simple you should only ever have one main form. So the splash screen should only be shown after your mainform has loaded. See [How to show a splashscreen in winforms](https://stackoverflow.com/questions/7955663/how-to-build-splash-screen-in-windows-forms-application). Note that this is [way easier to do in WPF](https://learn.microsoft.com/en-us/dotnet/desktop/wpf/app-development/how-to-add-a-splash-screen-to-a-wpf-application?view=netframeworkdesktop-4.8), and I would seriously consider using WPF for any new development, even if there is a learning curve. – JonasH Jul 17 '23 at 12:50

0 Answers0