2013-12-10 80 views
-4

我使用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

+0

可能会有一些非后台线程正在运行 - 这将阻止应用程序退出。你可能需要研究这些。附上一个调试器,看看表单关闭后是否有线程仍在运行。 – sthotakura

回答

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() 
} 
+0

所以thx的家伙。它的运行;) – user3088739

+3

为什么你需要在FormClosing中调用'Application.Exit()'?这没有意义。我没有失望,但我没有得到这个答案。我也不太了解这个问题。 –

+0

由于简单的原因形式是一个form3,这意味着它不是主要的明确?因此,应用程序正在运行另一个表单,因此他应该调用application.exit –

0

如果Form3实例是你的主要形式,这意味着如果Application.Run()被称为与Form3一个实例,然后一般过程将退出在关闭该表单。

如果Form3不是您的主要形式,则表示主窗体已隐藏,未关闭。


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

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

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

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

但是,如果你有这样的一些代码,一个更好的做法是使用一个Timer