Ray
Ray

Reputation: 4929

Winforms app does not shut down

I have a Winforms app that does not shut down when we close the main form. I.e. the process stays in Windows TaskManager. Here is the main program:

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

The main form has this code in its InitializeComponent:

this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.ServerSimulatorFormClosed);

And in the ServerSimulatorFormClosed code there is:

    private void ServerSimulatorFormClosed(object sender, FormClosedEventArgs e)
    {
        if (ThornhillServerSimulator != null)
            ThornhillServerSimulator.StopListening();
        if (RamqSimulator != null)
            RamqSimulator.StopListening();
        if (SaaqSimulator != null)
            SaaqSimulator.StopListening();
        if (DisSimulator != null)
            DisSimulator.StopListening();
        if (PharmaSpaceSimulator != null)
            PharmaSpaceSimulator.StopListening();
        if (DispenserSimulator != null)
            DispenserSimulator.StopListening();
        if (AlbertaBlueCrossServerSimulator != null)
            AlbertaBlueCrossServerSimulator.StopListening();
    }

Since the main form opens with Application.Run, I assume I need to call Application.Exit to close down the appliction. But where do I put it. This app has threads as well. Could it be that they are preventing the app from closing. If yes, how do I properly shut down the app?

Upvotes: 0

Views: 68

Answers (1)

xpda
xpda

Reputation: 15813

Try moving the StopListening calls from the Closed event to the Closing event.

Upvotes: 1

Related Questions