Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I would like to pass all the control of the program to the event handler (ConsoleCancelEventHandler). However, once I terminate the overall program by CTRL-C while Globals.object1.runprocess() is executing, the flow goes to the event handler as expected, but the main process continues, disposing of object1, before the handler can retrieve information from object1. The try catch block in the handler works correctly in checking on the object1.Status because the exception thrown until the status is available can be seen. This code only works correctly if I run in in debug mode, or if I add a delay right after Globals.object1.runprocess(). How do I ensure that once interrupted, the flow continues ONLY on the process inside the handler and not in main?

What I have tried:

C#
using System;
using ownlibraryfoo;

public static class Globals
{    
    public static foo_type object1;
}

class sample_cs
{
    static void Main(string[] args)
    {

        Console.CancelKeyPress += new ConsoleCancelEventHandler(Console_CancelKeyPress);

        try {
                       
            Globals.object1.initialize();

            Globals.object1.runprocess();           
      
            Globals.object1.Dispose();
            
        } catch (fooException e) {
                Console.WriteLine("Error code : " + e.ErrorCode + ". " + e.Message);
            }

        return;
    }

    static void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e)
    {           
        Console.WriteLine("Program Terminated Manually");
        e.Cancel = true;
        
        Globals.object1.Terminate();
        
        bool tryAgain = true;
        /* query solve status until available and handle exception when not available */
        while(tryAgain)
        {
            try
            {
                if (Globals.object1.Status == FOOINTERRUPTEDSTATUS)               
                {                    
                     Console.WriteLine(" OBJECTIVE = " + Globals.object1.internalattribute);  
                }
         
                tryAgain = false;
            }
            catch (FooException f)
            {
                /* the error output is optional */
                Console.WriteLine("Object1 Error code = " + f.ErrorCode);
                Console.WriteLine(f.Message);
            }
        }        
        Environment.Exit(0);
    }
}
Posted
Updated 23-Feb-21 3:48am
v2
Comments
Dave Kreskowiak 23-Feb-21 10:53am    
There's no way to tell because you haven't provided any details at all on what this "object1" process is.

The problem is going to be in the code of whatever object1 is. It's being told to Terminate, but is it responding to that call? There's no way for us to tell.

Another thought is that you're catching exceptions in the wrong place. The exceptions should be entirely caught in Main, not in the Ctrl-C handler. All the Ctrl-C handle should do is call Terminate. That's it. In your case, the Main method should handle all exceptions and Exit, not the Ctrl-C handler.
Richard MacCutchan 23-Feb-21 11:32am    
Your main method catches the exception, prints a message and terminates. And part of that termination process involves disposing of any objects that main has instantiated.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900