2017-01-23 32 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

好吧,我把错误的表达。 –