Winforms应用程序不会关闭

时间:2014-12-04 18:50:11

标签: multithreading winforms

我有一个Winforms应用程序,当我们关闭主窗体时它不会关闭。即该过程保留在Windows TaskManager中。这是主要的计划:

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());
    }
}

主窗体的InitializeComponent中包含此代码:

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

在ServerSimulatorFormClosed代码中有:

    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();
    }

由于主窗体使用Application.Run打开,我假设我需要调用Application.Exit来关闭应用程序。但是我把它放在哪里。这个程序也有线程。可能是因为他们阻止了应用关闭。如果是,我该如何正确关闭应用程序?

1 个答案:

答案 0 :(得分:1)

尝试将StopListening调用从Closed事件移动到Closing事件。