2014-12-04 43 views
0

我有一个Winforms应用程序,当我们关闭主窗体时不关闭。即该进程保留在Windows TaskManager中。这里是主程序:Winforms应用程序不关闭

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来关闭应用程序。但我把它放在哪里。这个应用程序也有线程。难道他们正在阻止应用程序关闭。如果是的话,我该如何正确关闭应用程序?

+0

相似的问题http://stackoverflow.com/questions/12977924/how-to-properly-exit-a-c-sharp-application – Sybren 2014-12-04 19:06:50

+0

那么,'ServerSimulatorFormClosed'中的哪些方法卡住了?另外,如果您注释掉整个通话组会发生什么情况? – 2014-12-04 19:22:15

+0

那些线程在调用'StopListening()'时结束了吗?如果他们不终止,那就是让你的流程保持开放。 – Jcl 2014-12-04 20:09:53

回答

1

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