У меня есть приложение 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, чтобы закрыть приложение. Но где я могу это выразить. Это приложение также имеет потоки. Может быть, они препятствуют закрытию приложения. Если да, как правильно закрыть приложение?
Аналогичный вопрос http://stackoverflow.com/questions/12977924/how-to-properly-exit-a-c-sharp-application – Sybren
Ну, какой из этих методов в «ServerSimulatorFormClosed» застревает? Кроме того, что произойдет, если вы закомментируете весь набор вызовов? –
Связаны ли эти потоки при вызове 'StopListening()'? Если они не прекращаются, это то, что удерживает ваш процесс открытым. – Jcl