表格关闭错误

时间:2013-12-10 21:50:24

标签: c# winforms

我正在使用C#进行表单应用程序。我的问题是,在表单关闭后,无法在任务管理器上停止?

我的代码:

private void Form3_FormClosing(object sender, FormClosingEventArgs e)
{
    DialogResult dr = MessageBox.Show("Gerçekten programı kapatmak istiyor musunuz?", "Closing event", MessageBoxButtons.YesNo);

    if (dr == DialogResult.No)
        e.Cancel = true;
    else
        e.Cancel = false;
}

像这样:

http://u1312.hizliresim.com/1j/b/vdbqd.png

2 个答案:

答案 0 :(得分:2)

关闭表单并不意味着您的申请将终止 Form3_FormClosing就像它说的那样:它关闭一个表单。如果您打开了其他表单,则该应用程序将不会终止。

private void Form3_FormClosing(object sender, FormClosingEventArgs e)
{
    DialogResult dr = MessageBox.Show("Gerçekten programı kapatmak istiyor musunuz?", "Closing event", MessageBoxButtons.YesNo);

    if (dr == DialogResult.No)
        e.Cancel = true;
    else
        // Here's what you need.
        Application.Exit();

       //or Environment.Exit() but not recommended would just kill the process. If some form has e.g. unsaved changes it would not have any chances to ask the user if he wants to save them. Also resources (database connections etc.) could not be released properly, files might not be flushed etc. 

       //Environment.Exit()
} 

答案 1 :(得分:0)

如果Form3的实例是您的主要表单,意味着如果使用Application.Run()的实例调用了Form3,则通常会在关闭该表单时退出。

如果Form3不是您的主要表单,则表示主表单已隐藏,而非已关闭。


我看到一个程序仍然潜伏在后面的另一个原因是由于自定义前台线程。

Thread t = new Thread( someMethodWithALoop );
t.Start();

如果此线程正在运行,则进程不会退出。您可以通过将线程标记为背景来解决它。

Thread t = new Thread( someMethodWithALoop );
t.IsBackground = true;
t.Start();

但是,如果您有类似的代码,更好的做法是使用Timer