2014-12-04 46 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事件。