2012-02-15 48 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();