2014-12-04 2 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

+0

Ну, какой из этих методов в «ServerSimulatorFormClosed» застревает? Кроме того, что произойдет, если вы закомментируете весь набор вызовов? –

+0

Связаны ли эти потоки при вызове 'StopListening()'? Если они не прекращаются, это то, что удерживает ваш процесс открытым. – Jcl

ответ

1

Попробуйте переместить вызовы StopListening из закрытого события в событие закрытия.

Смежные вопросы