3

This is the Program class:

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");
        }
    }
}

now when i click the "X" button to close my form, this is what i see in the output from 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).

But that still doesnt end the application, the process seem to still be open in visual studio as the "red square" aka "stop debugging" button is still waiting to be pressed. When i press it i get the following message:

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

Interestingly if I do a logoff from windows and come back the problem goes away, restarting visual studio doesn't solve the problem.

Facundo La Rocca
  • 3,786
  • 2
  • 25
  • 47
Joao Vitor
  • 169
  • 1
  • 9
  • This is not the problem raising the `COMException`. That probably causes the application not to be closed. Can you share the origin of that exception? – Patrick Hofman Jan 23 '17 at 15:55
  • show your code that closes the form – T McKeown Jan 23 '17 at 15:55
  • Take a look at [this question](http://stackoverflow.com/questions/4281425/how-to-avoid-a-system-runtime-interopservices-comexception) - it might shed some light on why you're getting the COMException - and how to find it. – Cullub Jan 23 '17 at 15:58
  • It looks like you messed up the close/dispose events of your form ? – Laurent Lequenne Jan 23 '17 at 16:00
  • I dont think my code has anything to do with it, it happens on a blank project as well, thats it, create the project and dont add any code. Logging off and logging in to my windows user solves the problem – Joao Vitor Jan 23 '17 at 16:01
  • Show the code for Form1, something is happening inside that is raising an exception. – Facundo La Rocca Jan 23 '17 at 16:06
  • @JoaoVitor - ??? "_Logging off and logging in to my windows user solves the problem_" makes no sense to me, please elaborate. – Joe Jan 23 '17 at 16:31
  • @Joe well basically it shouldnt make ANY sense at all. I save my project, i logoff from my windows user account, log back in, open the project again, and the form closes normally every time when i run it. IT DOESNT make any sense. Something gets refreshed in the process, no idea what it is. maybe a windows bug itself – Joao Vitor Jan 23 '17 at 16:35
  • 1
    @JoaoVitor - OK, so just to clarify, you fix the error by logging out of windows, then logging back in. What causes the error to happen again? – Joe Jan 23 '17 at 16:39
  • @Joe yes thats correct, i am so not sure about what causes that error again. it seems a little random to me, could be when i leave my machine on for some hours. Sorry i am completely blind for this. – Joao Vitor Jan 23 '17 at 16:50
  • @JoaoVitor I think we need the stacktrace to say more. – poizan42 Jan 23 '17 at 17:04
  • @JoaoVitor, I think you should include more information like (target framework, configuration, and platform). Maybe we could reproduce the problem. – 41686d6564 stands w. Palestine Jan 23 '17 at 17:19

1 Answers1

-3

Use MessageBox instead 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);
Serge V.
  • 3,377
  • 3
  • 20
  • 28