2017-01-23 33 views
3

這是程序類:C#WinForms應用程序犯規接近達成Program.Main()結束時

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

namespace TestClosingApp 
{ 
    static class Program 
    { 
     /// <summary> 
     /// The main entry point for the application. 
     /// </summary> 
     [STAThread] 
     static void Main() 
     { 
      Application.EnableVisualStyles(); 
      Application.SetCompatibleTextRenderingDefault(false); 
      Application.Run(new Form1()); 
      Console.WriteLine("I AM AT THE END OF THE MAIN"); 
     } 
    } 
} 

現在,當我點擊「X」按鈕關閉我的形式,這是我在看從2015年VS

I AM AT THE END OF THE MAIN 
Exception thrown: 'System.Runtime.InteropServices.COMException' in System.Windows.Forms.dll 
The thread 0x1c34 has exited with code 0 (0x0). 
The thread 0x2008 has exited with code 0 (0x0). 

但是,輸出仍然不結束該應用程序,過程似乎仍是在Visual Studio中的「紅方」開又名「停止調試」按鈕仍然在等待被按下。當我按下它,我得到以下信息:

The program '[8216] TestClosingApp.vshost.exe' has exited with code -1 (0xffffffff). 

有趣的是,如果我這樣做從Windows註銷,然後回來的問題消失,重新啓動Visual Studio中沒有解決的問題。

+0

這不是提高'COMException'問題。這可能會導致應用程序無法關閉。你能分享那個例外的起源嗎? –

+0

顯示你的代碼,關閉表格 –

+0

看看[這個問題](http://stackoverflow.com/questions/4281425/how-to-avoid-a-system-runtime-interopservices-comexception) - 它可能會脫落一些關於你爲什麼得到COMException的信息 - 以及如何找到它。 – Cullub

回答

-3

使用MessageBox代替Console.WriteLine("I AM AT THE END OF THE MAIN");

MessageBox.Show("I AM AT THE END OF THE MAIN", "My message", MessageBoxButtons.OK, MessageBoxIcon.Warning); 
+0

是的,您可以在調試時在[輸出窗口](https://msdn.microsoft.com/en-us/library/3hk6fby3.aspx)中顯示該消息。 –

+0

當然你可以調用Console.WriteLine,但輸出可能不會去任何地方。這與手頭的問題無關。 – poizan42

+0

好吧,我把錯誤的表達。 –