到达Program.Main()时,C#WinForms应用程序不会关闭

时间:2017-01-23 15:54:12

标签: c# exit formclosing

这是Program类:

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”按钮关闭我的表格时,这就是我在vs 2015的输出中所看到的

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并不能解决问题。

1 个答案:

答案 0 :(得分:-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);