2012-02-15 50 views
0

经过2个多小时的查找之后为什么当我关闭窗口时我的血腥过程不会退出。我终于发现它是主窗口中的问题(而不是在一个线程中,因为这可能也是问题)!但我仍然不知道为什么它错误。窗口关闭错误或e.cancel = false,但它完全关闭了

所以这段代码使得错误:

private void Window_Closing(object sender, CancelEventArgs e) 
{ 
    try 
    { 
     MessageBox.Show(e.Cancel.ToString()); // False always 

     if (formcontroler.formchampinfo != null) // THIS IS NULL so it won t go into this IF 
     { 
      formcontroler.formchampinfo.Close(); // never gets here so it doesn t matter what this do right ? 
     } 

     MessageBox.Show(e.Cancel.ToString()); // always false, just to check or it would get to this part tbh 
    } 
    catch (Exception ex) 
    { 
     throw (ex); // never gets thrown 
    } 
} 

这是REALY奇怪(我)!因为它会到达第二个消息框,并且e.cancel = FALSE,所以它不应该取消它,只是关闭并且该进程应该被终止(并且Visual Studio应该停止调试)。

无论如何它不会停止。它只是不断的过程ALIFE和我不知道为什么,如果我删除中间,如果还是将其替换为一个简单的办法,如:

private void Window_Closing(object sender, CancelEventArgs e) 
{ 
    try 
    { 
     MessageBox.Show(e.Cancel.ToString()); // False always 

     if (true == true) // just To test ofc 
     { 
      int lol = 5; 
      lol++; 
     } 

     MessageBox.Show(e.Cancel.ToString()); // always false, just to check or it would get to this part tbh 
    } 
    catch (Exception ex) 
    { 
     throw (ex); // never gets thrown 
    } 
} 

那么它的工作原理,并像它应该(该进程被杀死退出程序和Visual Studio停止调试

一些侧代码shoudn吨mattter我认为

class formcontroler 
{ 
    public static frmchampinfo formchampinfo; 
} 

的frmchampinfo是一个窗口,但大气压予加载它或declaterded它。(如formchampinfo =新frmchaminfo();) 这是一个错误还是发生在这里? ealy不知道为什么它不关闭我的代码。

解决对不起,我可以T回答这个问题到晚上7小时,但随后我睡着了。(因为我穿上吨得了100代表还)

奥凯使面色越来越深后我发现,IF语句创建一个在我的FormController类其他形式的(对不起,我奥斯卡最佳吨提供答案完整的代码,所以你coudn牛逼看着办吧):

class formcontroler 
     { 
      public static frmchampinfo formchampinfo; 
      public static Window activeform; 
      public static frmlog formlog = new frmlog(); 
    //... more code blabla 
     } 

的formlog“被制造”在这里。如果我将formcontroller.formlog.close()添加到代码中,那么它可以完全关闭。

private void Window_Closing(object sender, CancelEventArgs e) 
     { 
      try 
      { 
       MessageBox.Show(e.Cancel.ToString()); // False always 
       if (formcontroler.formchampinfo != null) // THIS IS NULL so it won t go into this IF 
       { 
        formcontroler.formchampinfo.Close(); // never gets here so it doesn t matter what this do right ? 

       } 
       formcontroler.formlog.Close(); // THIS FIXED IT... lame sorry for all your time. HoweveR I have learned something hope you did too. 
       MessageBox.Show(e.Cancel.ToString()); // always false, just to check or it would get to this part tbh 
      } 
      catch (Exception ex) 
      { 
       throw (ex); // never gets thrown 
      } 
     } 
+0

嗯,你离开了知识的最重要的部分,任何人能够帮助你。 'formcontroler','formchampinfo'和'formchampinfo.Close()' – 2012-02-15 20:29:12

+0

请寄出所有的代码如何声明表单并处理结束事件。 – Turbot 2012-02-15 20:31:21

+0

在if,第一个如果他永远不会进去的地方添加了信息。我如何申报这种形式?这只是一个窗口,是来自Visual Studio的“自动启动窗体”。 – Maximc 2012-02-15 20:33:05

回答

1

检查您的App类的ShutdownMode设置为OnMainWindowClose

您也可以明确地关闭应用程序:

Application.Current.Shutdown();