1

So i have a program that needs to do some cleanup before it exits, even if it is a forced exit. I know, hitting the power button is a forced exit where no cleanup can happen, but that would cause other, bigger issues too.

The Program hooks into a number of events to take care of the cleanup. My issue is that this does not work properly when compiled in Debug mode and this does not work correctly when compiled in Release mode and run from within VS (F5). In release mode, run from the console or from the file manager, everything does what it is supposed to.

When trying to debug the application if the Handler routine is triggered I get a nice dialog saying vshost has failed, looking for solution to the problem...

What do I have to do to make this code work in debug and release mode?

EDIT

Error: Error

exact steps take: 1) Press F5 2) wait a seconds 3) Click the [X] in the console window 4) Error, see linked image above

class Program
{
    public enum CtrlTypes
    {
        CTRL_C_EVENT = 0,
        CTRL_BREAK_EVENT,
        CTRL_CLOSE_EVENT,
        CTRL_LOGOFF_EVENT = 5,
        CTRL_SHUTDOWN_EVENT
    }

    static string conDB = "";

    [DllImport("Kernel32")]
    public static extern bool SetConsoleCtrlHandler(HandlerRoutine Handler, bool Add);
    public delegate bool HandlerRoutine(CtrlTypes CtrlType);
    private static bool ConsoleCtrlCheck(CtrlTypes ctrlType)
    {
        DatabaseAccess db = DatabaseAccess.Instance; //Issues Here

        // Cleanup code removed

        return true;
    }

    public static void ExitProcess(object sender, EventArgs args)
    {
#if DEBUG
        Debug("\n\n\nDebug Mode\nPress any key to continue...\n");
        Console.ReadKey(true);
#endif
    }

    static int Main(string[] args)
    {
        SetConsoleCtrlHandler(new HandlerRoutine(ConsoleCtrlCheck), true);
        AppDomain.CurrentDomain.ProcessExit += ExitProcess;

        // App code removed

        return 0;
    } // End main


    static void Debug(string text)
    {
        #if DEBUG
            Console.WriteLine(text);
        #endif
    }
}
Glorfindel
  • 19,729
  • 13
  • 67
  • 91
Justin808
  • 19,126
  • 41
  • 143
  • 241
  • possible dup: http://stackoverflow.com/questions/474679/capture-console-exit-c – kenny Sep 30 '10 at 20:11
  • possible duplicate of [.NET Console Application Exit Event](http://stackoverflow.com/questions/1119841/net-console-application-exit-event) – Randolpho Sep 30 '10 at 20:12
  • 1
    Not an exact duplicate.. this is a C# Console ablication – SwDevMan81 Sep 30 '10 at 20:14
  • What is the behaviour you expect and what do you actually get? I tried your code in the Debug version from within VS with the debugger attached but didn't notice any problem. – Dirk Vollmar Sep 30 '10 at 20:14
  • @SwDevMan81: which comment are you referring to? – BoltClock Sep 30 '10 at 20:18
  • No repro, no mention of the VS version, no idea. – Hans Passant Sep 30 '10 at 20:24
  • @0xA3: i would expect it to "just work" without issue. What I get is that the VSHOST32.EXE seems to crash as a result of the handler be evoked. and the code in the handler is not run. – Justin808 Sep 30 '10 at 20:28
  • @Hans Passant: VS version 10.0.30319.1 RTMRel Applciation is running on .Net 2.0 – Justin808 Sep 30 '10 at 20:29
  • Can you describe the exact steps that lead to the exception? You start the debug configuration with F5 and then? Are you getting the exception with the sample code above or with the full code (it says app code / clean up code removed)? – Dirk Vollmar Sep 30 '10 at 20:40
  • @0XA3: I have a lot of application code in there, its talking to a database, sending emails, etc. While that is all going on, I click the [X] in the title bar of the console application which triggers the handler. In the handler I have code that resets the database state to something usable rather than 1/2 processed. Everything works as expected as long as I'm not running in debug mode :) – Justin808 Sep 30 '10 at 20:46
  • This means the problem is related to the code that you are interrupting with your cleanup. The sample above doesn't repro the problem. Without seeing actually all the code it is difficult to guess what the actual problem is. You probably only see it in Debug mode because then your app is run via the vshost process. You can disable that process though in the VS option. – Dirk Vollmar Sep 30 '10 at 20:51
  • @0xA3: Seeing as its the VSHost process that is crashing (?) could the problem he in vshost32.exe rather than my code? – Justin808 Sep 30 '10 at 20:57
  • No, most likely it is an unhandled exception from your code, but without repro it's not possible to tell. – Dirk Vollmar Sep 30 '10 at 21:01
  • @0xA3: If I disconnect the handler (comment out the SetConsoleCtrlHandler) line, and close the application with the [X], it just closes, no errors at all, vshost32.exe doesn't crash either. The application just quits. Is there a way to try an determine where the unhandled exception is occurring? – Justin808 Sep 30 '10 at 21:08
  • @Justin808: I am facing a similar problem here with that code. In my case i don't get an exeption but i found that entering the ConsoleCtrlCheck function doesn't halt exiting, so program exits before all cleanup is done - might be your case, when trying to cleanup you end up getting a null reference cause the program exiting eliminated the reference in the meanwhile – 537mfb Aug 09 '12 at 10:20
  • @justin808: Try this: put a breakpoint on the first line inside the ConsoleCtrlCheck function and then run and exit the application. When it reaches the breakpoint it will stop. Don't press step over or step by step - just wait a couple of seconds - your program will exxit. During debug VSHost delays things a bit - ence you might get an exception while not getting it running normally – 537mfb Aug 09 '12 at 10:23

0 Answers0